Created
August 10, 2017 14:59
-
-
Save AkyunaAkish/72bf8be91cb8fab475fc1176dc5b6d70 to your computer and use it in GitHub Desktop.
updating_the_url_in_a_function_react_router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { withRouter } from 'react-router-dom'; | |
class SomeComponent extends Component { | |
constructor(props) { | |
super(props); | |
} | |
componentWillMount() { | |
const someConditional = this.props.history.location.pathname === '/something-wrong'; | |
if (someConditional) { | |
// push the new path to history to programmatically change your route | |
this.props.history.push('/'); | |
} | |
} | |
render() { | |
return ( | |
<div> | |
<h1>Some Content</h1> | |
</div> | |
); | |
} | |
} | |
export default withRouter(SomeComponent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment