MobX异步反应

Kom*_*omo 4 javascript asynchronous store mobx

嗨,我在商店中使用MobX,当计算值发生变化时,我需要进行异步反应:

class Store {
    @observable user;
    @observable something;

    @computed get firstParam () {
         return this.user && this.user.params[0];
    }

    async loadSomething () {
        reaction(
                () => this.firstParam,
                async (param) => {
                    const { data: something } = await axios.get(`url/${param}`);

                    runInAction('update state after fetching something', () => {
                        this.something = something;
                    });
                }
            );
     }

}
Run Code Online (Sandbox Code Playgroud)

我想知道使用when而不是reaction分开运行条件会有什么区别?

when(
    () => !!this.firstParam,
    async () => {
         // fetch using this.firstParam
    }
)
Run Code Online (Sandbox Code Playgroud)

mwe*_*ate 6

请注意,只when执行一次效果然后停止.因此,在您的情况下,数据只会被提取一次.