を反応させるのドキュメント上ここに:リアクト - KotlinラッパーSETSTATE方法
this.setState({
counter: this.state.counter + this.props.increment,
});
、代わりに:
https://facebook.github.io/react/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous
それは状態が非同期で更新されるため、次のコードを使用することが安全ではないと言います前の状態でパスし、次のような小道具:
this.setState(function(prevState, props) {
return {
counter: prevState.counter + props.increment
};
});
しかし、リアクト - Kotlinラッパーがここにあります:
//Located in the ReactComponent class in ReactComponent.kt
fun setState(builder: S.() -> Unit) {
...
}
の場合:
https://github.com/Kotlin/kotlin-fullstack-sample/tree/master/frontend/src/org/jetbrains/react
は状態オブジェクトに変数を変更する状態の拡張機能として渡された状態変化を持っています私はKotlinにこのよう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
}