How do I avoid re-rendering a connected React PureComponent due to mapDispatchToProps functions?
I generally advise to not try to recreate functions that capture props values like that, but instead have a handler method on your class that passes prop values to the action creator. I also advise that people not write mapDispatch
functions directly, but use the "object shorthand" for connect()
.
Example:
const actions = {addToStack : StackAction.addEpisodeToStack};
class MyComponent extends React.Component {
addToStack = () => {
this.props.addToStack(this.props.episodeId, this.props.stackId);
}
}
In your specific snippet, it looks like you're not even referencing any props values in mapDispatch
anyway, so there was no need to declare the ownProps
parameter. (connect
will only call a mapDispatch
function multiple times if the ownProps
parameter is requested. Otherwise, it only calls mapDispatch
once, when the component is created.)