Skip to content

Purpose of an Action #3501

Answered by urugator
omergilboa-zz asked this question in Q&A
Aug 15, 2022 · 2 comments · 3 replies
Discussion options

You must be logged in to vote

https://mobx.js.org/actions.html#updating-state-using-actions point 1.

const state = mobx.observable({ x: 0, y: 0 });

autorun(() => {
  console.log(state.x, state.y);
})

state.x = 1; // autorun immediately prints 0,1
state.y = 1; // autorun immediately prints 1,1

mobx.action(() => {
  state.x = 2; // autorun is only scheduled but not immediately invoked
  state.y = 2; // autorun is only scheduled but not immediately invoked
})() // scheduled autorun is invoked once and prints 2,2

Same goes for observer, but React does it's own batching, so you may not notice that.

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@omergilboa-zz
Comment options

Answer selected by omergilboa-zz
Comment options

You must be logged in to vote
2 replies
@omergilboa-zz
Comment options

@urugator
Comment options

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