Reputation: 59
I had an interview, where the interviewer asked me if redux uses context API internally. Pls provide a detailed description if yes. Thanks
Upvotes: 5
Views: 1888
Reputation: 44086
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.
Upvotes: 3
Reputation: 631
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).
Upvotes: 5
Reputation: 702
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.
Upvotes: 2