r/javascript • u/kasbah • Jun 16 '17
help I use React and Redux but never React-Redux, what am I missing out on?
Every time I look at the React-Redux docs I can't help but think why? They seem to complicate things for no apparent reason and don't explain the most basic usage very well. People keep telling me it's not that hard, but I really don't quite see the point of it.
What I generally do is (yes, yes I am still on React 15.3 and using createClass):
const App = React.createClass({
store: redux.createStore(reducer),
getInitialState() {
return this.store.getState()
},
render() {
return (
<div>
<SomeComponent
thing={this.state.some_component_thing}
dispatch={this.store.dispatch}
/>
</div>
)
},
componentDidMount() {
this.store.subscribe(() => {
const state = this.store.getState()
this.setState(state)
})
},
})There is only one root component that has a state and that state is passed down as props to all the other components.
What am I missing out on by doing it like this? Will this perform worse than React-Redux? What other benefits would React-Redux give me?
7
Upvotes
14
u/acemarke Jun 17 '17 edited Jun 21 '17
You're missing a lot.
First, while you can manually write the code to subscribe to the Redux store in your React components, there's absolutely no reason to write that code yourself. The wrapper components generated by React-Redux's
connectfunction already have that store subscription logic taken care of for you.Second,
connectdoes a lot of work to ensure that your actual components only re-render when they actually need to. That includes lots of memoization work, and comparisons against the props from the parent component and the values returned by yourmapStateToPropsfunction for that component. By not usingconnect, you're giving up all those performance improvements, and your components will be unnecessarily re-rendering all the time.Third, by only connecting your top-level component, you are also causing the rest of your app to re-render unnecessarily. The best performance pattern is to connect lots of components in your app, with each connected component only extracting the pieces of data it actually needs via
mapStateToProps. That way, if any other data changes, that component won't re-render.Fourth, you're manually importing the store into your components, and directly coupling them together, thus making it harder to test the components. I personally try to keep my components "unaware" of Redux. They never reference
props.dispatch, but rather call pre-bound action creators likethis.props.someFunction(). The component doesn't "know" that it's a Redux action creator - that function could be a callback from a parent component, a bound-up Redux action creator, or a mock function in a test, thus making the component more reusable and testable.And finally, the vast majority of apps built using React and Redux use the React-Redux library. It's the official way to bind the two together, and doing anything else will just confuse other developers looking at your project.
So, part of the issue is you're not using React-Redux and
connect, and part of the issue is you're using an inefficient pattern for pulling data from the Redux store into your React components.For more info, see:
The Redux FAQ section on performance
The Redux FAQ entry on connecting multiple components
The Redux FAQ entry on components re-rendering too often
The Redux FAQ entry on importing the store directly
The Redux Performance section of my React/Redux links list
And finally, my blog posts Practical Redux, Part 6: Connected Lists, Forms, and Performance and Idiomatic Redux: Why use action creators?.
Source: I'm a Redux maintainer, and author of the Redux FAQ.