React Mobx-从其他商店更改商店而不呈现观察者组件

问题描述 投票:1回答:1

我必须通过一家商店的@observable功能更新@action变量。当我从@action类中调用此component时,它将更改@observable变量并重新呈现@observer组件。但是,当我尝试通过使用回调从other store中的@action调用此@action时,@observable变量会更改,但@observer组件不会重新呈现。

一家商店:


class OneStore {
   @observable variable;

   @action
   setVariable(value) {
      this.variable = value;
   }
}

其他商店:

class OtherStore {

   @action
   setVariable(callBack, value) {
      callBack(value); //callBack is oneStore.setVariable
   }
}

@observer组件:

@inject('oneStore')
@observer
class ObserverComponent extends Component {

   render() {
      if (this.props.oneStore.variable) {
         return (
            <div> {this.props.oneStore.variable} <div/>
         );
      } else { return null; }
   }
}

我也尝试通过@computed get函数来获取变量,但仍然无法重新渲染。

reactjs mobx mobx-react
1个回答
0
投票

我试图添加绑定到动作-@action.bound,它确实起作用...

我认为嵌套回调后,OneStore的this被破坏了。

正确的代码是:

class OneStore {
   @observable variable;

   @action.bound
   setVariable(value) {
      this.variable = value;
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.