Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mobx - runInAction() usage. Why do we need it?

I have read various websites regarding this topic but I haven't managed to figure out why do we need the runInAction method and how exactly does it work.

Can someone explain to me runInAction functionality?

Thanks.

like image 883
user7479651 Avatar asked Jul 30 '19 11:07

user7479651


People also ask

What is runInAction?

runInAction<T> function Null safetyExecutes the mutation function fn within an Action . This ensures that all change notifications are fired only at the end of the Action block. Note that actions can be nested, in which case the notifications go out when the outermost Action completes.

What is MobX reaction?

Reactions are an important concept to understand, as it is where everything in MobX comes together. The goal of reactions is to model side effects that happen automatically. Their significance is in creating consumers for your observable state and automatically running side effects whenever something relevant changes.

How does MobX observable work?

Observables. Observables in MobX allow us to add observable capabilities to our data structures—like classes, objects, arrays—and make our properties observables. That means that our property stores a single value, and whenever the value of that property changes, MobX will keep track of the value of the property for us ...

Is MobX synchronous?

Mobx autorun is synchronous normally, but becomes non-synchronous when an action is running #1911.


1 Answers

The short answer is: you don't really need runInAction. You can write an application without using it, and it should work just fine.

But if you're working on a larger codebase, and you want to enforce some best practices, you can use the mobx feature "enforce actions / strict mode", which basically enforces that any modification to the state must happen inside of an action. This is useful because actions make it obvious why a piece of state changed, and they provide useful debugging information in the mobx devtools.

By using this configuration flag, mobx will throw an error if you try to modify the state outside of an action.


Alright, what is runInAction ?

Here's an example without runInAction:

loadWeather = city => {
  fetch(
    `https://abnormal-weather-api.herokuapp.com/cities/search?city=${city}`
  )
    .then(response => response.json())
    .then(data => {
      this.setWeatherData(data);   //   <==== here
    });
};


@action
setWeatherData = data => {
  this.weatherData = data;   
};

Since we are using strict mode, we had to define a new action just to set the weatherData.

This can get tedious pretty quickly, when having to define an action just to use it once.

Here comes runInAction to make it much shorter:

loadWeatherRunInThen = city => {
  fetch(`https://abnormal-weather-api.herokuapp.com/cities/search?city=${city}`)
    .then(response => response.json())
    .then(data => {
      runInAction(() => {
        this.weatherData = data;         // <====== We dont have to define an action
      });
    });
};

So basically, runInAction takes a piece of code and executes it in an anonymous action, instead of having to manually create an action for it.

For more information, check these links:

  • https://github.com/mobxjs/mobx/blob/mobx4and5/docs/refguide/action.md#runinactionname-thunk
  • https://github.com/mobxjs/mobx/blob/mobx4and5/docs/refguide/api.md#configure
  • https://www.leighhalliday.com/mobx-async-actions

Edit:

The answer above was around the days of Mobx 4.

For Mobx 6:

By default, MobX 6 and later require that you use actions to make changes to the state. However, you can configure MobX to disable this behavior.

Links to the new documentation:

https://mobx.js.org/actions.html#runinaction

https://mobx.js.org/actions.html#disabling-mandatory-actions-

https://mobx.js.org/configuration.html#enforceactions

like image 166
Moe Avatar answered Oct 11 '22 10:10

Moe