- MobX Quick Start Guide
- Pavan Podila Michel Weststrate
- 220字
- 2021-08-05 10:34:26
Quick recap on reactions
MobX offers a couple of ways to execute side-effects, but you have to identify which one fits your needs. Here is a quick round-up that can help you in making the right choice.
We have three ways of running side-effects:
- autorun( effect-function: () => {} ): Useful for long-running side-effects. The effect function executes immediately and also anytime the dependent observables (used within it) change. It returns a disposer function that can be used to cancel anytime.
- reaction( tracker-function: () => data, effect-function: (data) => {} ): Also for long-running side-effects. It executes the effect function only when the data returned by the tracker function is different. In other words, reaction() waits for a change in the observables before any side-effects are run. It also gives back a disposer function to cancel the effect prematurely.
- when( predicate-function: () => boolean, effect-function: () => {} ): Useful for one-off effects. The predicate function is evaluated anytime its dependent observables change. It executes the effect function only when the predicate function returns true. when() automatically disposes itself after running the effect function. There is a special form of when() that only takes in the predicate function and returns a promise. Use it with async-await for a simpler when().
推薦閱讀
- 廣電5G從入門到精通
- Hands-On Industrial Internet of Things
- Go Web Scraping Quick Start Guide
- 數字通信同步技術的MATLAB與FPGA實現:Altera/Verilog版(第2版)
- 物聯網與無線傳感器網絡
- 計算機網絡原理與應用技術
- 光纖通信系統與網絡(修訂版)
- 面向5G-Advanced的關鍵技術
- 計算機網絡技術及應用
- Web用戶查詢日志挖掘與應用
- 新媒體交互藝術
- 現場綜合化網絡運營與維護:運營商數字化轉型技術與實踐
- Hands-On Cloud:Native Microservices with Jakarta EE
- XSS跨站腳本攻擊剖析與防御
- 智能家庭網絡:技術、標準與應用實踐