Introduction

Managing states in a react native application can be a pain if the application is a large application and have lots of relationships with each component.

What is Redux?

Redux is a state management tool. Or according to the official documentation this is a predictable state container for JavaScript.

Purpose of this is, it helps to write applications that behave consistently, run in different environments and easy to test.

Use of Redux

Since data and login in react native flows unidirectional we have to add all the data and logic to the application itself if we want to access these data in another component. And for this we use Redux.

we can use contacts or pass down props instead of using redux in a small application with less relationships among the components, but it is more complicated to use the above-mentioned methods if we are dealing with more relationships.

Redux keeps all the data and logic in a separate place called the store and by using this store we can access all the stored data and logic from any component.

About Redux

There are three pillars part of this framework they are

  1. Store
  2. Action
  3. Reducer
  4. Store is basically a globalized state where you use to store all the data and logic
  5. Action is basically initializing the function that you are going to do

Ps: An Action is basically a function which returns and object

  • Reducer is the action change one state to another

Ps: we can combine reducers if we have more than one in a certain component.

  • We use Dispatch to run the defined actions (execute the actions).
  • We use providers from react-redux to make the store accessible throughout the app.
  • We use selectors from react-redux to access the reducers and actions.
READ  CI/CD Pipeline|Why teams should use them more often.

Pros and Cons

Pros

  • For a medium or large-scale progressive application, we have to use Redux.
  • Redux is easy to write If you are familiar with the syntax.
  • Total separation of data and presentation.

React native components don’t have the concept of state, so we are storing the data in Redux storage for good results.

  • Redux is more concerned in the state of the application rather than the presentation of the app (UI)
  • Never think of re-render

Since React native rendering are immutable the Redux Re-render renders all the relevant components no matter how many times it renders.

Cons

  • This uses lots of boiler plates to do even a simple task.
  • Using this in a small app with less relationships among the components is a waste.

Conclusion

In conclusion Redux can be very useful if you are building a large application where one data set is being used in multiple components and it will be a waste to use Redux in a small application with less relationships among components.

Sources

https://www.gistia.com/beginners-guide-redux/