I am trying to redirect user to home page conditionally before component mounts:
componentWillMount(){
console.log("componentWillMount is called.")
let userHasNotChosenOpportunity = true
if (userHasNotChosenOpportunity) {
this.props.history.push("/home")
}
}
I have two problems:
componentWillMount
never gets calledcomponentWillMount
is deprecated and there seems to be no alternative to execute code before component mounts.
Any suggestions?
You should try calling it on
componentDidMount
or if you for some reason really need to usecomponentWillMount
you can useUNSAFE_componentWillMount
but as indicated it is unsafe