【问题标题】:Change property or state in store "1" from action in store "2"从存储“2”中的操作更改存储“1”中的属性或状态
【发布时间】:2017-04-26 12:37:55
【问题描述】:

是否可以通过不同商店的操作来更改某些商店的状态。我通常在所有组件上使用 Inject,但我猜它们不适用于商店。

我有一个专门的商店来控制通知弹出的状态。更改它的打开/关闭状态以及来自我执行 ajax 调用的所有其他商店的操作的消息会很棒。

【问题讨论】:

    标签: javascript notifications store inject mobx


    【解决方案1】:

    将通知存储导入其他存储并以这种方式调用某些操作可能是最简单的:

    // notificationStore.js
    class NotificationStore {
      @observable notif = { state: 'closed', msg: '' };
      @action changeState(state, msg) {
        this.notif.state = state;
        this.notif.msg = msg;
      }
    }
    
    export default new NotificationStore();
    
    // otherStore.js
    import notificationStore from './notificationStore.js'
    
    class OtherStore {
      @action someAction(someData) {
        // Some code...
        notificationStore.changeState('open', 'someAction complete');
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-08
      • 2021-05-12
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多