如果计算视图发生变化,如何调用 mobx-state-tree 操作?
How to call an mobx-state-tree action if a computed view changes?
基本上我想做的是在计算视图发生变化时调用一个动作。
const Store = types
.views(self => ({
get computedValue() {
return somethingComputed;
}
}))
.actions(self => ({
doSomething() {
doSomeStuffUsingComputedValue(self.computedValue);
}
}));
我想在 computedValue 更改时调用 doSomething 操作。我无法控制更新 computedValue 的内容。
知道如何实现吗?
你可以使用 mobx autorun
, when
or reaction
来完成。
例如,每次 computedValue
变化都会触发此反应:
import { reaction } from 'mobx'
reaction(
() => store.computedValue,
() => store.doSomething()
)
您可以在 mobx 文档中阅读有关这些 mobx 实用程序的更多信息:https://mobx.js.org
基本上我想做的是在计算视图发生变化时调用一个动作。
const Store = types
.views(self => ({
get computedValue() {
return somethingComputed;
}
}))
.actions(self => ({
doSomething() {
doSomeStuffUsingComputedValue(self.computedValue);
}
}));
我想在 computedValue 更改时调用 doSomething 操作。我无法控制更新 computedValue 的内容。
知道如何实现吗?
你可以使用 mobx autorun
, when
or reaction
来完成。
例如,每次 computedValue
变化都会触发此反应:
import { reaction } from 'mobx'
reaction(
() => store.computedValue,
() => store.doSomething()
)
您可以在 mobx 文档中阅读有关这些 mobx 实用程序的更多信息:https://mobx.js.org