· 2 min read · originally on LinkedIn ↗
What I learned when I tried to replace Redux with Context + Hooks
Context and hooks look like a lighter Redux, until every interaction re-renders half the app. When Context is enough, and when it isn't.
Redux being Redux has a lot of boilerplate and needs a bunch of libraries to implement, and it increases the bundle size of an application. React's built-in Context API with hooks, on the other hand, is much easier to set up and adds nothing to the bundle.
So I tried replacing Redux with Context + hooks. It didn't go well.
Why replacing Redux was a bad idea
- Providers nested deeper and deeper. Every slice of state got its own context provider, and the tree turned into a pyramid of them.
- Everything re-rendered. Any change to one of these context objects caused almost every component to re-render on each user interaction.
- State got hard to maintain. As the project grew more complex, it became hard to reason about where state lived and who changed it.
When to use Context + hooks, and when not to
After some research, this is what I learned:
- For low-frequency updates like locale, theme changes or user authentication, React Context is perfectly fine.
- For complex state with high-frequency updates, React Context isn't a good solution.
- That's because Context triggers a re-render on each update, and optimising that manually is really tough.
- That's where a solution like Redux is much easier to work with.
Written by Mohammed Mudassir, senior software engineer in Bengaluru. Turning ideas into scalable products.