I've discussed useContext and useReducer and have created an application using both in my blog series; now is the time to compare useContext + useReducer vs Redux!
- Brief Summary of Redux
- Recall React and React Hooks
- Which to use
Brief Summary of Redux
Redux is a JavaScript library designed to manage global state in applications. It includes additional packages such as React Redux, which integrates with React, and React Toolkit, a useful tool for simplifying and enhancing coding efficiency.
The core concept of Redux involves a centralized store for the state, with predefined events called "actions" responsible for updating this state.
The patterns and tools provided by Redux make it easier to understand when, where, why, and how the state in your application is being updated, and how your application logic will behave when those changes occur.
Redux is a pattern and library for managing and updating application state, using events called "actions". It serves as a centralized store for state that needs to be used across your entire application, with rules ensuring that the state can only be updated in a predictable fashion.
Recall React and React Hooks
React follows a single data flow where data is passed from parent to child components. Through useContext, data defined in the context can be accessed from anywhere within components wrapped with the Provider. However, useContext alone does not handle state management; developers need to define state management using useReducer.
According to the React documentation, before React Hooks were released in React 16.8 in 2019, a class handled the state and other React features. Redux was then released in 2015 and gained popularity. Subsequently, in 2019, React Hooks, including useState and useReducer, were introduced in React 16.8, offering an alternative and more modern approach to handling state in functional components.
Now, which to use?
Redux is more useful when:
- You have large amounts of application state that are needed in many places in the app
- The app state is updated frequently over time
- The logic to update that state may be complex
- The app has a medium or large-sized codebase, and might be worked on by many people
Use Cases:
- Comprehensive e-commerce
- Real-time messaging or chat application
- Workflow management system for a content creation platform
- Enterprise-level project management tool used by multiple teams within a company
There is a statement in bold font right below it:
Not all apps need Redux. Take some time to think about the kind of app you're building, and decide what tools would be best to help solve the problems you're working on.
Additionally, it shares some blog posts (one of them was written by the Redux creator, Dan Abramov!), where, (to me personally,) most of the articles imply the usage of Redux only when needed.
One of the authors of the blogs points out a crucial thing that we should always keep in mind, so I'll share it here:
you need to understand exactly what problems you are trying to solve in your own application right now, and pick the tools that solve your problem best. not because someone else said you should use them, not because they’re popular, but because this is what works best for me in this particular situation.
I was the one who used to choose Redux over React Hooks simply because I learned Redux at school, and useReducer and useContext seemed challenging. Now that I understand the history and usage of both, it's time to confidently consider why I would choose one over the other.
Top comments (0)