在反应中如何观察Mobx观察函数之外的可观察物?

时间:2019-01-16 02:11:32

标签: reactjs mobx-react

我有一家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

1 个答案:

答案 0 :(得分:1)

componentDidUpdate(prevProps){
    if(prevProps.myStore.myvariable !== this.props.myStore.myvariable){
        // update mydata
    }
}

https://reactjs.org/docs/react-component.html#componentdidupdate

相关问题