Skip to content

storeSubscription context object is lost when a component don't have a mapStateToProps #577

@ericm546

Description

@ericm546

Hi,

If I have the following components:

const Child = connect(childMapState, childMapDispatch, () => <div />);
const Middle = connect(null, middleMapDispatch, () => <Child />);
const Parent = connect(parentMapState, parentMapDispatch, () => <Middle />);

Because, the middle component has no mapStateToProps, the value of shouldHandleStateChanges will be false in connectAdvanced and getChildContext() will put undefined as the storeSubscription value sent to the Child component.

This cause my application to have the following warning, when an action cause some components to be unmounted:
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the Connect(Child) component.

I was not able to reproduce it in a separated project, but it fix the problem if I either put an "if" inside the getChildContext function of the connectAdvanced component:

if(this.subscription ) {
    return {[subscriptionKey]: this.subscription}
} else {
  return {}
}

or if I put a function returning an empty object as the first parameter to connect.

Note that I tested it in react-redux 5.0 and 5.0.1 and this was working before in 4.X.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions