withintheruins14
withintheruins14

Reputation: 417

Why is componentWillUnmount firing after next components componentWillMount?

I have a React/Redux application with two components. I need to clear a portion of redux state when the first component unmounts, because the second component will error with the state in that form. I've tried to dispatch an action clearing the chunk of state when the first component unmounts, but the second component begins mounting before the first components componentWillUnmount method is called. When I view dispatched actions in redux-logger, I see the second component dispatching actions from componentWillMount and then componentWillUnmount actions from the previous component called.

This is not the expected behavior is it? I am also using react-router v4. Thanks!

Upvotes: 2

Views: 3522

Answers (1)

withintheruins14
withintheruins14

Reputation: 417

Since React v16, the componentWillUnmount hook can fire asynchronously.

This means that you can't make any assumptions about the order (or timings) of the invocations of these hooks cross-component.

Upvotes: 4

Related Questions