Be able to return null from mapStateToProps
See original GitHub issueI 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
.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top GitHub Comments
@markerikson Actually it is not a rare case for react-native apps at all, all of them use something like react-navigation and screens are not unmounted in tab bar or when you push new to the stack navigator. So all tabs, and their inner stacks are always mounted, connected to redux and js performance suffers a lot - js animations lag,lists are not rendered during fast scroll, button handlers work with delay, and the more screens mounted, the more js lags.
@timdorr It could be implemented for example with new option
isConnected: (ownProps: OwnProps) => boolean
, that is called when ownProps change. When false - connect unsubscribes from store, when changes to true it subscribes and re-evaluates mSTP etc.Probably it would be also nice to have option like
isDisabled: (ownProps: OwnProps) => boolean
, that acts same as first one, but also stops all re-renderings.Naming is discussable.
EDIT: or may be
isConnected: (state, ownProps) => boolean
Also, note that unless the mSTP function depends on ownProps, it won’t be called if just the props change. This side-stepping of mSTP is already built-in in that case.