我有一家mobx商店
class MyStore {
@observable
public myvariable = ""
一个视图,该视图应在myvariable
值更改时更新其数据
@inject("myStore")
@observer
class MyView extends React.Component<any, any> {
@observable
public mydata;
// inside this class I have to know if `myvariable` value changed
// if it does, I have to fetch some data to assign to `mydata` so
// that I can render that new data
我不想将mydata
放在商店中,因为我有很多视图,每个视图都有不同类型的数据,如果myvariable
值发生更改,这些数据可以更新。我只想更新当前显示的视图的mydata
答案 0 :(得分:1)
componentDidUpdate(prevProps){
if(prevProps.myStore.myvariable !== this.props.myStore.myvariable){
// update mydata
}
}
https://reactjs.org/docs/react-component.html#componentdidupdate