What & Why Redux ?

 Introduction

Redux is  a javascript library which can be used with any framework React , Angular etc. it is used to store some data globally which is used to share state across multiple components.

Now , you might be confusing that in react we shared state in component with useState and many more hooks . So why using redux at first place ?

Separation of states

Separation of state are generally on the basis of  what state is used between the components and where they are shared across the whole react app.
We will look at what separation of state in redux and see how this came to rescue and why we generally use it instead of hooks provided by react.

1)  Local State

The state which is used when taking input of the used for the form field . The local state is limited to the single component and useState hook is generally used to maintain local state as they are not complex to maintain

2) Cross - Component State

The state which is used in multiple components for example - when you want to make a modal , so it can share multiple components that closing , opening and many more functionalities that modal can have . The state is shared across multiple components are called cross-component state .We generally do prop drilling or prop chaining for this approach or use react context hook


3) App wide State

The state which is used in all components are generally define as app- wide state . For example - the state which is used for letting know if the user is authenticated or not and affects multiple components by showing header according to the authentication etc.
We generally do prop drilling or prop chaining for this approach or use react context hook

Redux generally used in Cross - Component state and App-wide state . But you might be wondering why we need specifically redux when we can use react context hook.  This question might be revolving in your head , isn't it?

Redux  Vs  React Context

React Context does share state across multiple components but it does has some potential disadvantages which is not a complete replacement for flux like state propagation which is redux

1) Complex State Management

With using React context in large application or enterprise applications , you can end up in multiple providers and it can become complex to maintain where you have so much large file which cause endless debugging and  hard to maintain . Look over the below image and you can understand the complexity of using the react context for the complex state management.

Well we can use where the project is small and easy to maintain where the react context is used for the shared state between components.


2) Performance

React context gives performance when you have an application , it may not update or change the state with high frequency and does not reflect the state . Briefly when you have state that changes frequently in your application , it does not reflect the change in your application with that frequency of update.

Conclusion

Redux help you write code that behaves consistently in your application and it doesn't limited to only react . You can use it anywhere you wanted and also have large ecosystem which will help redux to use easily in you project . Redux help us to centralized our state and make it use widely in all components .

So ,this is redux and the reason why we should redux and where we should redux.

Thank you for reading  :)





Comments

Popular posts from this blog

Learn React Router v6 ( Part - 2)

Why use Typescript ?

What is Single Page Application Routing ?