question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Be able to return null from mapStateToProps

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gentleecommented, Feb 2, 2020

@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

0reactions
timdorrcommented, Jan 30, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MapStateToProps Returning Null in react-redux - Stack Overflow
I have successfully implemented mapDispatchedToProps. But mapStateToProps function is returning Null. My code is given below. MeatShopReducer
Read more >
Connect: Extracting Data with mapStateToProps - React Redux
The first argument to a mapStateToProps function is the entire Redux store state (the same value returned by a call to store.getState() )....
Read more >
Connect | React Redux
The returns of mapStateToProps and mapDispatchToProps are referred to internally ... to store updates, pass null or undefined in place of mapStateToProps ....
Read more >
Redux state shows data, but props returns null inside the state ...
Redux state shows data, but props returns null inside the state of ... default connect(mapStateToProps, mapDispatchToProps )(RegisterFinal);.
Read more >
React Redux connect(): When and how to use it
</div> ) } const mapStateToProps = function(state) { return { profile: ... that you may not be able to implement in your application....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found