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. With redux being introduced we can tackle this problem there are few setbacks in redux if you don’t know what you are doing, or you have no idea how redux work.
What is Redux?
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.
What Redux do?
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.
There are three pillars in Redux they are
- Store is basically a globalized state where you use to store all the data and logic
- 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.
Pros and Cons of Redux
- 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.
- Redux uses lots of boiler plates to do even a simple task.
- Using Redux in a small app with less relationships among the components is a waste.
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.