Skip to content

Use componentWillMount instead of componentDidMount for subscriptions #320

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
}
}

componentDidMount() {
componentWillMount() {
this.trySubscribe()
}

Expand Down
68 changes: 64 additions & 4 deletions test/components/connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1398,17 +1398,17 @@ describe('React', () => {
const target = TestUtils.findRenderedComponentWithType(tree, Passthrough)
const wrapper = TestUtils.findRenderedComponentWithType(tree, StatefulWrapper)

expect(mapStateSpy.calls.length).toBe(2)
expect(mapDispatchSpy.calls.length).toBe(2)
expect(mapStateSpy.calls.length).toBe(1)
expect(mapDispatchSpy.calls.length).toBe(1)
expect(target.props.statefulValue).toEqual('foo')

// Impure update
const storeGetter = wrapper.state.storeGetter
storeGetter.storeKey = 'bar'
wrapper.setState({ storeGetter })

expect(mapStateSpy.calls.length).toBe(3)
expect(mapDispatchSpy.calls.length).toBe(3)
expect(mapStateSpy.calls.length).toBe(2)
expect(mapDispatchSpy.calls.length).toBe(2)
expect(target.props.statefulValue).toEqual('bar')
})

Expand Down Expand Up @@ -1639,5 +1639,65 @@ describe('React', () => {
expect(mapStateCalls).toBe(2)
expect(renderCalls).toBe(1)
})

it('should give new state to children before parents', (done) => {
function valueSet(prev = 'a', action) {
return action.type === 'SET'
? action.value
: prev
}

const store = createStore(valueSet)

@connect(state => ({ component: state }))
class Parent extends Component {
render() {
if (this.props.component === 'a') {
return <A />
} else {
return <B />
}
}
}

const aSpy = expect.createSpy()

@connect(state => {
aSpy(state)
return { component: state }
})
class A extends Component {
render() {
return <div>{this.props.component}</div>
}
}

const bSpy = expect.createSpy()

@connect(state => {
bSpy(state)
return { component: state }
})
class B extends Component {
render() {
return <div>{this.props.component}</div>
}
}

TestUtils.renderIntoDocument(
<ProviderMock store={store}>
<Parent />
</ProviderMock>
)

setTimeout(() => {
store.dispatch({ type: 'SET', value: 'b' })
expect(aSpy.calls.length).toBe(1)
expect(aSpy.calls[0].arguments).toEqual([ 'a' ])
expect(bSpy.calls.length).toBe(1)
expect(bSpy.calls[0].arguments).toEqual([ 'b' ])
done()
}, 0)
})
})
})