Is MobX a good alternative to Redux?

MobX

Is MobX a good alternative to Redux?

More and more often, in new projects, MobX appears in place of the king of state management, which is undoubtedly Redux. According to many, it offers a clearer and more efficient way to manage the application state.

Redux logo

What is Redux?

Redux is a library for managing the application state. It is an implementation of Flux architecture created by Meta (former Facebook). The main idea is to create "Single Source Of Truth" as a single store. The store is built from a reducer, which is a function that returns a changed state under the influence of dispatcher actions.

Reducer should meet the assumptions of "pure function", which means that for a given input state, we always get the same result on the output. The previously mentioned actions are usually objects containing information needed to cause a specific change. Dispatcher is a function that passes the action to a reducer.

If we want to use store in React application, we need an additional react-redux library as a connector that allows us to communicate with Redux. Usually, we wrap the application with a provider to which the store is passed, and components communicate via hooks like useSelector and useDispatch, or HOC (higher-order component) connect.

Mobx logo

What is MobX?

MobX is a library for managing an application’s state. It is based on decorators that add additional functionality to objects by transforming them into observable state. The construction of the store is not overly complicated. It consists of a JS class (but not necessarily) in which we define the fields and methods that we decorate in the constructor with makeObservable or makeAutoObservable.

Common decorators include observable, computed, and action. Observable transforms an object field into a value that allows the library to report changes in state. Computed, as opposed to observable, is used for values that are calculated from the current state. Action is a decorator that informs the library that a particular method is used to change the state. Communication in React applications is done by calling methods and accessing fields. Store is passed to components through context.

In order for components to detect state changes, they need to be wrapped with an observer function from the additional library mobx-react-lite.


Readability

Because of its more complicated structure, Redux can be difficult to figure out what's actually going on in the code - especially for first-time users. Setting up Redux where we have a single store also makes it difficult when we want to clearly separate its various parts into domains. MobX allows you to create multiple instances which do not necessarily have to wrap the entire application. Communication and store structure is based on mechanisms that seem natural to people familiar with JavaScript. The situation looks even better when using TypeScript because a lot of code can be simplified into decorators from TS syntax.


Performance

According to a benchmark that was posted on Twitter in most cases MobX is able to boast better results than Redux. Check here

MobX vs Redux

What does Redux have in its defense?

The great thing about Redux in this comparision is that it is still a much more popular solution, and with that comes community support. It is also a battle-tested solution and is developed by top developers backed by one of the tech giants that is Meta.


Is MobX a good alternative then?

Is it worth betting on MobX, then? It depends, of course. The decision should always be based on the needs of the project and the team that implements it. Choosing a less popular library always carries a higher risk of encountering problems that no one has yet solved. On the other hand, lower complexity allows you to avoid most problems. It is definitely more difficult to incorrectly design a store based on MobX than it is with Redux.

click here to go to the contact form
Content

Got a project?

Let's talk!

__wf_zastrzeżone_dziedziczyć
Offtop
Infoshare Katowice 2024: Summary
arrow icon
12.3.2024
1 min read
Offtop
GITEX Global 2024: Insights
arrow icon
10.25.2024
1 min read
Code
What is JSON?
arrow icon
10.29.2024
2 min read
Code
Code refactoring – What is it?
arrow icon
10.24.2024
4 min read
AI
Secure AI - Advantages
arrow icon
7.12.2024
2 min read
Technologies
What is AWS?
arrow icon
4.1.2024
2 min read
Technologies
What is HTML?
arrow icon
3.21.2024
2 min read
Technologies
What is TypeScript?
arrow icon
3.20.2024
3 min read
Technologies
What is PHP?
arrow icon
3.19.2024
1 min read
Technologies
What is Swift?
arrow icon
3.18.2024
5 min read
Technologies
What is Kotlin?
arrow icon
3.16.2024
4 min read
Technologies
What is JAVA?
arrow icon
3.13.2024
2 min read
Technologies
What is React Native?
arrow icon
3.13.2024
3 min read
Technologies
What is React.js?
arrow icon
3.13.2024
2 min read
Technologies
What is Node.js?
arrow icon
3.13.2024
1 min read
Technologies
What is JavaScript?
arrow icon
3.13.2024
1 min read
Knowledge hub
What is a fullstack developer?
arrow icon
3.13.2024
1 min read
Knowledge hub
What is frontend?
arrow icon
3.13.2024
2 min read
Knowledge hub
What is backend?
arrow icon
3.13.2024
2 min read
IT
How to get started in IT?
arrow icon
3.6.2023
7 min read
IT
WEB3 - What is it? Introduction
arrow icon
2.21.2023
4 min read
UX/UI
UX Design - a guide for programmers
arrow icon
1.18.2023
4 min read
Business
Team Augmentation- Benefits!
arrow icon
1.4.2023
11 min read
Business
How to choose a programming company?
arrow icon
12.22.2022
8 min read
IT
How do programmers test?
arrow icon
12.18.2022
2 min read
IT
How to find good programmers?
arrow icon
12.15.2022
4 min read
Startup
What is a startup?
arrow icon
12.7.2022
7 min read
Code
Rust is the future of Server Side
arrow icon
12.1.2022
1 min read
IT
How to make a mobile app?
arrow icon
11.30.2022
5 min read
Business
How to work in various time zones?
arrow icon
11.27.2022
7 min read
Business
Where to Invest money in 2021?
arrow icon
10.31.2022
4 min read
IT
IT outsourcing – what is it?
arrow icon
10.25.2022
4 min read