如何在反应中观察渲染函数之外的 mobx observables?
How to observe mobx observables outside the render function in 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
当前显示的视图
componentDidUpdate(prevProps){
if(prevProps.myStore.myvariable !== this.props.myStore.myvariable){
// update mydata
}
}
https://reactjs.org/docs/react-component.html#componentdidupdate
我有一个 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
当前显示的视图
componentDidUpdate(prevProps){
if(prevProps.myStore.myvariable !== this.props.myStore.myvariable){
// update mydata
}
}
https://reactjs.org/docs/react-component.html#componentdidupdate