You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@observeronaclassislikeasugarsyntaxformakingtherenderfunctionintheclasscomponentareactionwhichwillhappenwheneverarelatedobservablevalueischanged.importDevToolsfrom'mobx-react-devtools'anduseitintherenderfunction<DevTools></DevTools>thenyouwillseethreebuttonsshownonyourcomponent
three functions: seewhichcomponentgetsupdated;seetheobserverandobservablerelations;seemoredetailedinformationintheconsole
Derive computed values and manage side effects with MobX reactions
Core parts: Actions, observable state, computed values, and reactions.
Computed values are not allowed to produce side effects like changing state or making network requests.
By default MobX doesn't try to keep computed values up to date. The reason for that is that MobX always tries to defer the computation of computed properties until they are needed by IO and side effects.
If you ask for a value which has been observed by reaction(values used in the last computed path), you can just get the value because it has cache. If you ask for a value which has not been observed or not used in the last computed path, it will be reevaluated and return the latest value.
If b is computed from a, but b is not observed, then whenever a updates its value, b will not update, unless you directly ask for the value of b, then it will be evaluated.
a 会影响到 b 的值的计算,但是 b 未被 observe,因此 a 更新值并不会导致 b 的值更新,除非主动调用 b,就能获取到 b 的最新值
Use observable objects, arrays, and maps to store state in MobX
transaction(()=>{t.unit='K';t.unit='F';})actionautomaticallyapplythetransactionfunctionforgroupingstatemodificationsuseStrict(true)thenyoucannotmodifystatein any otherwaysbutusingactions
@action('update unit')itwillshowupintheconsoleonceweenabledthechangeloginmobxdevtools.
Handle user input and asynchronous actions with MobX
Nothing new.
Connect MobX observer components to the store with the React Provider
import{Provider}from'mobx-react'consttemp=observable([])// some kind of store<Providertemperatures={temp}></Provider>
const t = observable(['temperatures'], ({temperatures}) =>(<ul>
...
</ul>
))
@observer(['temperatures']) // get temperatures from props without explicitly passing it from the parent component
class inputView extends React.Component {
...
@actiondoSomething=()=>{this.props.temperatures...}}
Write custom MobX reactions with when and autorun
when(()=>{returnTrueOrFalse()},()=>{doSomethingOnce()// when parameter one return true})constt=observable({})autorun(()=>{doSomethingUsingObservedValue(t)// run at once and whenever observed value changes})
The text was updated successfully, but these errors were encountered:
SuperAL
changed the title
[Learning notes] Mobx in React
Mobx in React
Nov 25, 2019
Mobx in React
Sync the UI with the app state using MobX observable and observer in React
Analyze React components with MobX-React devtools
Derive computed values and manage side effects with MobX reactions
Use observable objects, arrays, and maps to store state in MobX
Use MobX actions to change and guard state
Pass observable data through props in MobX
Handle user input and asynchronous actions with MobX
Nothing new.
Connect MobX observer components to the store with the React Provider
Write custom MobX reactions with when and autorun
The text was updated successfully, but these errors were encountered: