Description
I have isFocused
prop (from react-navigation) that indicates if my component is currently visible.
When my component is not focused, I want to disable connection to redux. For example I have tab-bar and when I switch tabs I don't want to unmount component but I also don't want to run mapStateProps
and to re-render component. Same for when I push component to stack navigation.
Currently:
The only way I guess is to pass option areMergedPropsEqual
and return true
if isFocused
is false. But this still required calculation mapStateToProps
and merging props, and just more complex and annoying to implement.
How it should be:
function mapStateToProps(state, ownProps) {
if (!ownProps.isFocused) return null
return {
....
}
}
If mapStateToProps
receives null
it should stop updating component props and re-rendering.
Similar approach is used for React.Component.getDerivedStateFromProps
.