初期状態がストア状態と同じに設定されているコンポーネントがあります。また、コンポーネントは、ストアで発生するすべての変更に帰属します。特定のストアプロパティの変更をリッスンします
// Inital component's state is set to be equal to a Store state
constructor(props) {
super(props);
this.state = EntityStore.getState();
this.entitiesOnChange = this.entitiesOnChange.bind(this);
}
// Subscribe to any Store's change
componentDidMount() {
EntityStore.listen(this.entitiesOnChange);
}
// Update the state with the new one
entitiesOnChange(nextState) {
this.setState(nextState);
}
私の目標は、コンポーネントをストアの特定のプロパティにサブスクライブすることです。
entitiesOnChange
でチェックを試みましたが、this.state
は既にストアの状態(nextState
)で最新であることがわかりました。
// Update the state with the new one only if a specefic `propery` is changed
entitiesOnChange(nextState) {
// Here is the problem. `this.state` is already up to date.
if (this.state.property !== nextState.property) {
this.setState(nextState);
}
}
それでは、どのように、私は特定のストアのプロパティに私のコンポーネントを購読することができます両方の状態は、したがって、発生していない再レンダリング、同じであるので、また次のコード例では(私が試したものを)this.setState(nextState)
は、呼び出されません?
ドウはあなたがEntityStoreに状態を更新あなたのcoode内の他の場所を持っています変化する? –
私は持っていますが、別のコンポーネント(上記の質問で使用されたコンポーネントではありません)にあります。 –
その状態はすでに最新の状態になっている可能性はありますか?あなたのnextStateプロパティに本当にnextStateが含まれていますか? (現在のものではない) –