Categories
Mobile

Redux vs Recoil: Choosing the right state management library for React Native

[ad_1]

State management is one of the most important skills every front-end or mobile app developer should master. Not only does it make the application more fluid or performant, but it also makes the code more easily maintainable when good tools are used. In the React Native ecosystem, you can find Redux, which is the most popular library used for state management, even ahead of React’s Context API. Redux is followed closely by Recoil, which (unlike Redux) is very easy to set up. Which of these two state management solutions should you choose for your next project?

In this article, we’ll compare these two state management solutions by using both of them to build a simple counter application with React Native.

Screenshot from 2022-03-01 15-06-23.png#center

Redux vs Recoil: Architecture

Redux architecture

The Redux architecture relies on one rule: All of your application states live in one container. To change the state, you’ll have to create a new state based on the current state and a requested change. This is why Redux has these three main components:

  • A store that holds all of your app’s state
  • An action, which is immutable data that describes a state change
  • A reducer that changes the app’s state using the current state and action

Redux comes with some problems, though, including the following:

  • Steep learning curve
  • Too much boilerplate code
  • Involves restructuring your project
  • Lack of concurrent mode support
  • General approach is not React-like
  • Difficult to achieve code splitting
  • No built-in async support

Recoil architecture

Recoil is a brand-new experimental JavaScript state management library that addresses many of the problems developers face when developing large applications using the existing Context API. Recoil has two main components — atoms and selectors.

Atoms are units of the state. They are also updatable and subscribable. This means that when an atom is updated, each subscribed component is re-rendered with the new value.

Selectors are pure functions that accept atoms or other selectors as arguments. When these upstream atoms or selectors are updated, the selector function is re-evaluated.

Components can subscribe to selectors just like they can to atoms. They are then re-rendered when a selector changes. Selectors can transform the atom state either synchronously or asynchronously.