是否可以在状态更改后检索初始状态?F.ex:
React.createClass({
getInitialState: function() {
return { foo: 'bar' }
},
componentWillMount: function() {
this.setState({ foo: 'foo' })
},
componentDidMount: function() {
// get the initial state "bar" ?
}
})
我在文件里什么都找不到。当然,我可以将值保存在一个外部变量中,但是如果可以将initialstate作为一个可以重用的"config“对象来处理,我只是好奇。
发布于 2014-07-09 00:22:50
不,初始状态没有存储--但是如果要重新执行函数,可以调用this.getInitialState()
。
发布于 2014-07-09 13:42:04
只需将初始状态保存在变量中:
React.createClass({
initialState: { foo: 'bar' },
getInitialState : function () {
return this.initialState;
},
componentWillMount: function () {
this.setState({ foo: 'foo' })
},
componentDidMount : function () {
console.log(this.initialState.foo); // Logs 'bar'
}
});
https://stackoverflow.com/questions/24647910
复制相似问题