[英]What's the difference between @computed and @action in MobX?
What's the difference between @computed
and @action
in MobX?什么之间的区别
@computed
和@action
在MobX?
They both are functions, so what is difference between them?它们都是函数,那么它们之间有什么区别呢?
@computed is used when the function will 'compute' the return value from existing information.当函数将从现有信息“计算”返回值时使用@computed 。
@action is used for functions that will change to the existing information (state), and prompts 'observers' to access the latest version of the relevant 'observable(s)'. @action用于将更改为现有信息(状态)的函数,并提示“观察者”访问相关“观察者”的最新版本。
The difference is stated in the documentation .差异在文档中说明。
action行动
Usage: action(fn) or action (annotation)
用法:动作(fn)或动作(注释)
Use on functions that intend to modify the state.
用于打算修改状态的函数。
computed计算
Usage: computed(fn, options?) or computed(options?) (annotation)
用法:计算(fn,选项?)或计算(选项?)(注释)
Creates an observable value that is derived from other observables, but won't be recomputed unless one of the underlying observables changes.
创建一个派生自其他 observable 的 observable 值,但除非底层 observable 之一发生变化,否则不会重新计算。
So computed doesn't do anything other than observing the state.因此,除了观察状态之外,计算不会做任何事情。 codesandbox
代码沙盒
action actually modify the state. action 实际上是修改状态。 action example
动作示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.