Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react context vs redux vs hooks, which one should consider and how each one is different

React has released the Context API and React Hooks but most of us are familiar with Redux, which one we should consider.

What is the purpose of using React Hooks and the React Context API? Please explain in detail how React Redux, Hooks and the Context API are different from each other.

like image 709
Vaibhav S Avatar asked Jun 11 '19 05:06

Vaibhav S


People also ask

Should I use React context or Redux?

If you're only using Redux to avoid passing down props, you can replace it with Context API. Context is great for sharing trivial pieces of state between components. Redux is much more powerful and provides a set of handy features that Context doesn't have.

Do Hooks and context replace Redux?

But now it's possible to replace Redux with React Hooks and the Context API. In this tutorial, you're going to learn a new way of handling state in your React projects, without writing excessive code or installing a bunch of libraries — as is the case with Redux.

Should I use Redux with React Hooks?

tip. We recommend using the React-Redux hooks API as the default approach in your React components. The existing connect API still works and will continue to be supported, but the hooks API is simpler and works better with TypeScript.


1 Answers

React Context is used to store state and share it across multiple components. It is especially useful when you have a deep tree of components and do not want to pass down state as props across multiple levels of components. Contexts in React consist of a Provider (where you set the value of the context) and a Consumer (where you get the value).

React Hooks provide a useContext Hook, which is another way to access context values. useContext replaces the Consumer component.

Redux is a state management library. It does a lot more than simply storing state with a set/get interface as Contexts do. Internally, Redux actually uses React Context to store its state. However, it additionally forces you to change state via actions. This makes sense if your state changes are complex (e.g. a single action should change multiple parts of your state). In a complex application, Redux can prevent bugs and inconsistent state.

As a rule of thumb, you should use React Context as long as your state changes are simple. If you run into problems where it is hard to keep multiple parts of your state in sync, it might make sense to consider Redux.

Whether you want to use Context Consumers or Hooks is totally up to you and a matter of taste. The advantage of using Hooks is that you can consume multiple contexts more easily without having a deep component tree.

Context Consumers:

   <AuthenticationContext.Consumer>
       {user => (
           <LanguageContext.Consumer>
               {language => (
                   <StatusContext.Consumer>
                       {status => (
                           ...
                       )}
                   </StatusContext.Consumer>
               )}
           </LanguageContext.Consumer>
       )}
    </AuthenticationContext.Consumer>

useContext Hooks:

const user = useContext(AuthenticationContext)
const language = useContext(LanguageContext)
const status = useContext(StatusContext)

As for the Providers, they are the same whether you use Hooks or not.

like image 180
omnidan Avatar answered Sep 28 '22 02:09

omnidan