Answer the question
In order to leave comments, you need to log in
React + Redux: what are the benefits of connecting each container separately with connect?
There is an application in which there is old and new code written by different programmers. I have currently accepted a job after them and am studying the code they wrote. One programmer preferred to "connect" Dispatch and State globally, in the "top" application container:
// App/Containers/AppContainer.js
...
const AppContainer = connect(mapStateToProps, mapDispatchToProps)(App);
export default AppContainer;
// App/Containers/SubContainers/SomeSubContainer.jsx
...
@connect(
state => ({
app: {
lang: state.page.blabla.language,
...
},
page: {
meta: {
tags: state.page.metaTags,
...
},
},
banners: state.banners,
...
}),
dispatch => ({
actions: bindActionCreators(actions, dispatch),
}),
)
class SomeSubContainer extends Component {
...\
Answer the question
In order to leave comments, you need to log in
first question:
when changes are made in the store (global state), all components that are subscribed to these changes and in which there is no explicit prohibition on this through shouldComponentUpdate will be redrawn. That is, if the parent is redrawn, then all its descendants will be redrawn. Class? (sarcasm, of course not a class). Therefore, connecting in "subcontainers", as you say, is more productive: only those pieces (parent + children) that were subscribed to a piece (part) of data from the global store will be redrawn.
the second question: this is how the react-redux library works and in particular the connect function, which seems to "glue" your sample data from the store into the props of the connected component. (using the capabilities of the reactive components property - context ). Look through the react dev tools and you will see that automatically, as a result of the connect function, the parent Connected(ComponentName) appears in which the necessary properties appear (through the context) and which are thrown down into the child (into the component you have already written manually) props .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question