Redux has a lot of boilerplate code. With this approach we can access any state and dispatch any actions with a single map state to props, which gives every component ALL state and actions. Are there any performance / maintainability issues with this approach?
Sai Kishore Komanduri
Engineering an eGovernance Product | Hashnode Alumnus | I love pixel art
If you're using React, the "recommended" way would be to use the <Provider> component that comes along with the
react-reduxlibrary, and have all of your app components as children inside the Provider component.ReactDOM.render( <Provider store={createStore(todoApp)}> <TodoApp /> </Provider>, document.getElementById('root') );What this does is it makes the store available to all of the children components through context. So you could do something like
const state = this.context.store.getState()in one of your components. But instead of doing this, it is recommended to useconnect()provided byreact-reduxconnect()and performanceSince state is anyway available to all of the children components, you can have your
mapStateToPropsfunction in your container components, and return only those parts of the state that could be passed on as props to the corresponding presenter components.function mapStateToProps(state) { const { todos, visibilityFilter } = state; return { todos, visibilityFilter } } function mapDispatchToProps(dispatch) { ... export default connect(mapStateToProps, mapDispatchToProps)(TodoApp);For a better performant Redux, if you remember only one thing, let it be this:
mapStateToPropsis the newrender; called on every store update ... and in this context, theconnect()function (which returns a higher order component that wraps around your presenter component) already optimises for updates based on any changes in the part of the state it passes down as props to the presenter component.So it only makes sense to pass only the required parts of the state as props through
mapStateToProps. So if there are no changes to these props,connect()will optimise for whether the component should update or not.If you want a detailed outlook into React/Redux perf. concepts, this GitHub issue serves as a great FAQ. This presentation on the same topic, is also awesome.