Skip to content

Is it appropriate to wrap functions that don't mutate observables with the action #3844

Answered by urugator
Brandon-Ln asked this question in Q&A
Discussion options

You must be logged in to vote

Basically everything should be either action or computed. computed must be pure function, so everything else is action.

class CounterStore {
  constructor() {
    this.counter = 0;

    makeObservable(this, {
      counter: observable,
      increment: action,
      submit: action, // <--
    });
  }
  /*....*/
  async submit() {
    //  this already runs in action, no need for extra `runInAction` before first await, but also no harm in using it
    const params = this.serializeParams();
    await saveServiceApi(params); // interact with server
    runInAction(() => {
       // In between awaits the runInAction is required. See `flow` for an alternative:
       // https://mobx.js.org/acti…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@urugator
Comment options

@Brandon-Ln
Comment options

Answer selected by Brandon-Ln
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