Reactjs setState更改构造函数类型
我发现,当通过Reactjs setState更改构造函数类型,reactjs,Reactjs,我发现,当通过setState设置类实例时,它会丢失构造函数并成为普通对象。我在文档中看到setState将值与当前状态合并,可能无法处理复杂类型。那么,状态应该始终是普通的内置类型吗 ====代码=== /** @jsx React.DOM */ //=============== transpiled through babeljs.io ======// function _classCallCheck(instance, Constructor) { if (!(instance
setState
设置类实例时,它会丢失构造函数并成为普通对象。我在文档中看到setState
将值与当前状态合并,可能无法处理复杂类型。那么,状态应该始终是普通的内置类型吗
====代码===
/** @jsx React.DOM */
//=============== transpiled through babeljs.io ======//
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Data = (function () {
function Data() {
_classCallCheck(this, Data);
}
Data.prototype.isLoading = function isLoading() {
return this.__state == 2;
};
return Data;
})();
//============
var Hello = React.createClass({
getInitialState: function() {
return new Data();
},
render: function() {
return (
<div>
State: <pre>{JSON.stringify(this.state)} proto: {this.state.constructor.name} </pre>
<button onClick={this.setXsimple}>setState</button>
</div>
);
},
setXsimple: function() {
this.setState(new Data());
}
});
React.renderComponent(<Hello name="World" />, document.body);
/**@jsx React.DOM*/
//================通过babeljs.io传输======//
函数_classCallCheck(实例,构造函数){if(!(构造函数的实例instanceof)){throw new TypeError(“无法将类作为函数调用”);}
变量数据=(函数(){
函数数据(){
_classCallCheck(这个,数据);
}
Data.prototype.isLoading=函数isLoading(){
返回此值。uu状态==2;
};
返回数据;
})();
//============
var Hello=React.createClass({
getInitialState:函数(){
返回新数据();
},
render:function(){
返回(
状态:{JSON.stringify(this.State)}协议:{this.State.constructor.name}
设定状态
);
},
setXsimple:function(){
this.setState(新数据());
}
});
React.renderComponent(,document.body);
是,此.state
应为普通JavaScript对象。但是,可以将复杂类型作为该对象的值:
/**@jsx React.DOM*/
//================通过babeljs.io传输======//
函数_classCallCheck(实例,构造函数){if(!(构造函数的实例instanceof)){throw new TypeError(“无法将类作为函数调用”);}
变量数据=(函数(){
函数数据(){
_classCallCheck(这个,数据);
}
Data.prototype.isLoading=函数isLoading(){
返回此值。uu状态==2;
};
返回数据;
})();
//============
var Hello=React.createClass({
getInitialState:函数(){
返回{data:newdata()};
},
render:function(){
返回(
状态:{JSON.stringify(this.State)}协议:{this.State.data.constructor.name}
设定状态
);
},
setXsimple:function(){
this.setState({data:new data()});
}
});
是,此.state
应为普通JavaScript对象。但是,可以将复杂类型作为该对象的值:
/**@jsx React.DOM*/
//================通过babeljs.io传输======//
函数_classCallCheck(实例,构造函数){if(!(构造函数的实例instanceof)){throw new TypeError(“无法将类作为函数调用”);}
变量数据=(函数(){
函数数据(){
_classCallCheck(这个,数据);
}
Data.prototype.isLoading=函数isLoading(){
返回此值。uu状态==2;
};
返回数据;
})();
//============
var Hello=React.createClass({
getInitialState:函数(){
返回{data:newdata()};
},
render:function(){
返回(
状态:{JSON.stringify(this.State)}协议:{this.State.data.constructor.name}
设定状态
);
},
setXsimple:function(){
this.setState({data:new data()});
}
});
是,此.state
应为普通JavaScript对象。但是,可以将复杂类型作为该对象的值:
/**@jsx React.DOM*/
//================通过babeljs.io传输======//
函数_classCallCheck(实例,构造函数){if(!(构造函数的实例instanceof)){throw new TypeError(“无法将类作为函数调用”);}
变量数据=(函数(){
函数数据(){
_classCallCheck(这个,数据);
}
Data.prototype.isLoading=函数isLoading(){
返回此值。uu状态==2;
};
返回数据;
})();
//============
var Hello=React.createClass({
getInitialState:函数(){
返回{data:newdata()};
},
render:function(){
返回(
状态:{JSON.stringify(this.State)}协议:{this.State.data.constructor.name}
设定状态
);
},
setXsimple:function(){
this.setState({data:new data()});
}
});
是,此.state
应为普通JavaScript对象。但是,可以将复杂类型作为该对象的值:
/**@jsx React.DOM*/
//================通过babeljs.io传输======//
函数_classCallCheck(实例,构造函数){if(!(构造函数的实例instanceof)){throw new TypeError(“无法将类作为函数调用”);}
变量数据=(函数(){
函数数据(){
_classCallCheck(这个,数据);
}
Data.prototype.isLoading=函数isLoading(){
返回此值。uu状态==2;
};
返回数据;
})();
//============
var Hello=React.createClass({
getInitialState:函数(){
返回{data:newdata()};
},
render:function(){
返回(
状态:{JSON.stringify(this.State)}协议:{this.State.data.constructor.name}
设定状态
);
},
setXsimple:function(){
this.setState({data:new data()});
}
});
来自:
第一个参数可以是一个对象(包含零个或多个
更新)或返回对象的函数(状态和属性)
包含要更新的键
我假设它们在这里指的是普通对象,而不是类或数组。例如来自:
第一个参数可以是一个对象(包含零个或多个
更新)或返回对象的函数(状态和属性)
包含要更新的键
我假设它们在这里指的是普通对象,而不是类或数组。例如来自:
第一个参数可以是一个对象(包含零个或多个
更新)或返回对象的函数(状态和属性)
包含要更新的键
我假设它们在这里指的是普通对象,而不是类或数组。例如来自:
第一个参数可以是一个对象(包含零个或多个
更新)或(状态和道具的)功能
/** @jsx React.DOM */
//=============== transpiled through babeljs.io ======//
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Data = (function () {
function Data() {
_classCallCheck(this, Data);
}
Data.prototype.isLoading = function isLoading() {
return this.__state == 2;
};
return Data;
})();
//============
var Hello = React.createClass({
getInitialState: function() {
return { data: new Data() };
},
render: function() {
return (
<div>
State: <pre>{JSON.stringify(this.state)} proto: {this.state.data.constructor.name} </pre>
<button onClick={this.setXsimple}>setState</button>
</div>
);
},
setXsimple: function() {
this.setState({ data: new Data() });
}
});