When creating a new React application, we think about redux as a state management solution as a default choice. Let’s have a look at the Context API as a possible replacement.
If you’re building a new React app today, your instincts may urge you to choose Redux. But should you start with Redux? That has turned into a legitimate question these days. The state management capabilities of React have vastly increased in recent versions. I’d want to discuss a more straightforward technique that relies on the Context API in this post.
LET’S TAKE A LOOK BACK
WHY DID WE ADOPT REDUX?
It wasn’t always the case that frontend apps used an organized approach to state transitions. When we were still using jQuery, I recall changing many locations in an application. That was not a pleasant encounter.
Here comes Redux to rescue, and in comparison, Redux was an outstanding solution:
- An object represents the whole state of the User Interface (UI).
- Any action that may change the state is a data-contained object.
- To change the state, those acts are ingested by pure functions (reducers)
- We structure the data in the state using selectors so that our components may consume it quickly.
It now appears self-evident. It was, nevertheless, excellent at the time.
All of the factors that got us here remain valid. However, the situation has altered nowadays.
The ecosystem of React has changed a lot recently. Two other additions have altered the terrain and created new opportunities:
- Hooks
- Context
It’s easier than ever to manage state across a hierarchical tree and trigger side effects using those two. So simple that I believe Redux is no longer required in many circumstances.
Most frontend apps lack the rich data flow that supports the use of Redux. If all you’re doing is getting data from an API and performing some simple interactions, Redux is overkill now.
OVER-ABSTRACTION IS A PITFALL THAT MANY PEOPLE FALL INTO
Forms are a fair instance of something close to elaborating this scenario. I’ve used Redux to create sophisticated forms with the aid of various supporting frameworks. Things become more complicated as they become more complex. And for what purpose? Finally, the state conveyed in a form is usually somewhat local.
Using Redux to model something like a form may be a painful experience. Is the state genuinely global? Frequently, a specialist library will do the job done. For this situation, Formik comes to the rescue. It’s designed specifically for that purpose and will meet almost any criteria you can think of.
USE CONTEXT API AS AN ALTERNATIVE
Read the rest of the article here: https://www.mushfiqweb.com/why-should-we-use-context-api-instead-of-redux-for-new-react-apps/
Top comments (0)