它们究竟意味着什么?如果我理解正确it,计算新的状态,当我不能使用this.state
,除非我通过一个函数作为第一个参数setState()
:状态更新可能是异步的
// Wrong
this.setState({a: f(this.state)});
// Correct
this.setState(prevState => {return {a: f(prevState)}});
但我可以用this.state
来决定做什么:
if (this.state.a)
this.setState({b: 2});
道具怎么样?
// Correct or wrong?
this.setState({name: f(this.props)});
按理我不能指望this.state
调用this.setState
后改变:
this.setState({a: 1});
console.log(this.state.a); // not necessarily 1
然后,说我有一个用户列表。以及选择在那里我可以做一个用户当前:
export default class App extends React.Component {
...
setCurrentUserOption(option) {
this.setState({currentUserOption: option});
if (option)
ls('currentUserOption', option);
else
ls.remove('currentUserOption');
}
handleAddUser(user) {
const nUsers = this.state.users.length;
this.setState(prevState => {
return {users: prevState.users.concat(user)};
},() => {
// here we might expect any number of users
// but if first user was added, deleted and added again
// two callbacks will be called and setCurrentUserOption
// will eventually get passed a correct value
// make first user added current
if (! nUsers)
this.setCurrentUserOption(this.userToOption(user));
});
}
handleChangeUser(user) {
this.setState(prevState => {
return {users: prevState.users.map(u => u.id == user.id ? user : u)};
},() => {
// again, we might expect any state here
// but a sequence of callback will do the right thing
// in the end
// update value if current user was changed
if (_.get(this.state, 'currentUserOption.value') == user.id)
this.setCurrentUserOption(this.userToOption(user));
});
}
handleDeleteUser(id) {
this.setState(prevState => {
return {users: _.reject(prevState.users, {id})};
},() => {
// same here
// choose first user if current one was deleted
if (_.get(this.state, 'currentUserOption.value') == id)
this.setCurrentUserOption(this.userToOption(this.state.users[0]));
});
}
...
}
是否所有的回调批量更改状态后顺序执行的应用?
关于第二个想法,setCurrentUserOption
基本上就像setState
。它会将更改排入this.state
。即使回调被依次调用,我也不能依靠this.state
被以前的回调改变,我能吗?所以它可能是最好不要提取setCurrentUserOption
方法:
handleAddUser(user) {
const nUsers = this.state.users.length;
this.setState(prevState => {
let state = {users: prevState.users.concat(user)};
if (! nUsers) {
state['currentUserOption'] = this.userToOption(user);
this.saveCurrentUserOption(state['currentUserOption']);
}
return state;
});
}
saveCurrentUserOption(option) {
if (option)
ls('currentUserOption', option);
else
ls.remove('currentUserOption');
}
这样,我得到排队的改变来currentUserOption
是免费的。
只需双重检查你是否使用了任何状态管理框架,如redux或flux?答案可能会有所不同,具体取决于我要去的 – aug
。但现在我想让它不用'redux'工作。 –