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.
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.
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.
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 ...
Mobx autorun is synchronous normally, but becomes non-synchronous when an action is running #1911.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With