Home > Enterprise >  Redux and React context together
Redux and React context together

Time:01-10

  • I am building a web app where redux is configured and app is fairly large.
  • Now I want to store some user preferences which will be available as part of an API respopse.

As this data is required to the majority of components I am planning to store data in the context and wrap application using the context.

I have Few questions regarding approach.

  • Will considering context impact the performance?
  • As Redux is already configured which internally uses the Context. So should we continue to use redux for user data.
  • Is it good practice to use Redux and Context together.

CodePudding user response:

Context and Redux are very different tools that solve different problems, with some overlap.

Context is not a "state management" tool. It's a Dependency Injection mechanism, whose only purpose is to make a single value accessible to a nested tree of React components. It's up to you to decide what that value is, and how it's created. Typically, that's done using data from React component state, ie, useState and useReducer. So, you're actually doing all the "state management" yourself - Context just gives you a way to pass it down the tree.

Redux is a library and a pattern for separating your state update logic from the rest of your app, and making it easy to trace when/where/why/how your state has changed. It also gives your whole app the ability to access any piece of state in any component.

In addition, there are some distinct differences between how Context and (React-)Redux pass along updates. Context has some major perf limitations - in particular, any component that consumes a context will be forced to re-render, even if it only cares about part of the context value.

So, yes, you can use them both in the same app, in the same way you can use a hammer and a screwdriver together on the same construction job.

For more details, see my posts:

CodePudding user response:

Will considering context impact the performance?

Since you want to share user preferences that (I guess) do not change often, you could use a React context to share that data.

But performance issues arise when you put multiple different data in one React context that update at different rates. This is because every component that uses the context will be rerendered even if the part of the context it is interessted in did not change. In this case you can split the context and use one context for each part of the data.

Since you want to use the context to share an application state, you should use Redux. In Redux you use selectors to select a part of the application state. This useSelector hook is implemented in a way that it only triggers a rerender of the component if the selected part changes. You can even pass it an equality function if you want to change the way state change is detected.

As Redux is already configured which internally uses the Context. So should we continue to use redux for user data.

I would say: yes, continue with Redux.

Since you already use Redux you should not spread your application state management over different concepts. Put the user settings in the Redux store (like any other application state) and don't handle them special.

Is it good practice to use Redux and Context together

Well, Redux is based on the React context. So if you use Redux it is already a good practice.

If you mean using both for application state management, I think you should go the Redux or the React context way. Mixing them makes it harder to understand where state is managed.

  •  Tags:  
  • Related