Skip to content

action VS autoActions best practice #3531

Answered by urugator
RoCat asked this question in Q&A
Discussion options

You must be logged in to vote

Mutating state as part of autorun/reaction is discouraged:
https://mobx.js.org/reactions.html#use-reactions-sparingly
If you still want to do so I would go with

autorun(() => {
  // yada yada
  runInAction(() => {
     dateMobx.setDateTo('XXX')
  })
})
// or (the effect is implicitly action)
reaction(() => `${foo.year}-${foo.month}-${foo.day}`, dateString => dateMobx.setDateTo(dateString))

Or another solution I didn't mentioned ?

makeAutoObservable(this, { setDateTo: action });

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@RoCat
Comment options

Answer selected by RoCat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants