Advanced react js interview questions for beginners to experienced JavaScript developers who want to improve their careers to the next level.
Two common react, redux interview questions
- Why did you choose redux over context?
- What is the difference between context and redux?
It’s important to understand that 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.
A context is a great tool by itself, and I use it frequently in my own apps. But, Context doesn’t “replace Redux”. Sure, you can use both of them to pass data down, but they’re not the same thing. It’s like asking “Can I replace a hammer with a screwdriver?”. No, they’re different tools, and you use them to solve different problems.