0
在反应文档浏览:阵营,科特林包装的setState方法
https://facebook.github.io/react/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous
它说,下面的代码是不安全的使用,因为状态是异步更新:
this.setState({
counter: this.state.counter + this.props.increment,
});
,而是以通过之前的状态和道具如下:
this.setState(function(prevState, props) {
return {
counter: prevState.counter + props.increment
};
});
但是,做出反应,科特林包装设在这里:
https://github.com/Kotlin/kotlin-fullstack-sample/tree/master/frontend/src/org/jetbrains/react
已通过作为国家的一个扩展功能,修改状态对象的变量的状态变化:
//Located in the ReactComponent class in ReactComponent.kt
fun setState(builder: S.() -> Unit) {
...
}
如果我叫setState
功能像这样在科特林:
setState {
counter: state.counter + props.increment
}
是不是等同于上述不安全的方法? React-Kotlin包装器中是否需要这样执行?
fun setState(builder: S.(prevState: S, props: P) -> Unit) {
...
}
然后这样调用?
setState { prevState, props ->
counter: prevState.counter + props.increment
}