私は定期的に更新されるコンポーネントを持っています。コンポーネントの更新時に内部状態を更新する必要があるこのコンポーネントの外部でユーティリティ関数を構築したいと思います。例えばReactコンポーネントがいつ更新されたかを確認する方法は?
:私はSample
関数にUtility
関数を渡すと、持っていた場合、私はこれを達成することができます知っている
class Sample extends React.Component {
constructor(props) {
super(props);
this.state = { counter: 1 };
}
}
class Utility {
constructor(componentInstance) {
this.internalNum = // computedInstance's internal counter state (how?)
// and when the component has updated, I'd like to change the internalNum (how?)
}
}
はSample
Utility
更新のリアクトライフサイクルメソッドに基づいていますが、私はしたくありませんそのような依存関係をSample
に導入する。
おそらく、ここでより良いデザインパターンがあるかもしれません...もしこれがうまくいけば、どのように動作しますか?
「サンプル」の複数のインスタンスをレンダリングするとどうなりますか? –