Reputation: 85775
I have some code that will run on componentWillUnmount()
but I only want it run if they go back to the previous page. If they go forward to the next page I don't want what is inside the componentWillUnmount
to run.
I am using React Router 4 but when I check it in the componentWillUnmount
it still has not updated to whatever the next url is.
componentWillUnmount() {
const props = this.props;
const location = props.location;
}
Upvotes: 1
Views: 950
Reputation: 4987
React Router provides a history object which you can use to set some variables before the transition to a new location.
Try something like this:
componentDidMount() {
this.props.history.block((location, action) => {
this.isGoingBack = action === 'POP';
})
}
componentWillUnmount() {
if (this.isGoingBack) {
...
}
}
You might need to check the location
aswell.
Upvotes: 2