Is Redux internally using context API?
Asked Answered
D

3

5

I had an interview, where the interviewer asked me if redux uses context API internally. Pls provide a detailed description if yes. Thanks

Diadem answered 27/8, 2021 at 7:10 Comment(1)
If I cleared your doubt, please accept my answer: my_answerNessa
N
5

Redux doesn't use context api, it's a javascript library for state management. You can use Redux with any application- like (react, angular, vue, svelte, etc). However, for using redux with react applications, you need react-redux which is the official react bindings for redux.

react-redux uses React's context api to make the redux store accessible to all components. The reason why you need react-redux is because, React uses virtual dom - which means you can't manipulate the DOM directly. In other libraries like angular, we use the NgRx library (For angular).

Nessa answered 27/8, 2021 at 7:34 Comment(1)
which source???Sarah
C
3

Yes, but differently than if you would be doing it by yourself.

If you were to use Context manually, you would usually do value propagation - passing the current state value down the tree. This can lead to very inefficient rerendering behaviour. See this article for a deeper explanation.

React-Redux on the other hand uses context for dependency injection. The store is passed down the tree and will never change, never triggering a rerender. All the rest is done by Redux internally, using manual subscriptions. This is much more performant.

Cheltenham answered 27/8, 2021 at 8:33 Comment(0)
P
2

Internally, React Redux uses React's "context" feature to make the Redux store accessible to deeply nested connected components. As of React Redux version 6, this is normally handled by a single default context object instance generated by React.createContext(), called ReactReduxContext.

Source

Patrickpatrilateral answered 27/8, 2021 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.