Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AkyunaAkish/72bf8be91cb8fab475fc1176dc5b6d70 to your computer and use it in GitHub Desktop.
Save AkyunaAkish/72bf8be91cb8fab475fc1176dc5b6d70 to your computer and use it in GitHub Desktop.
updating_the_url_in_a_function_react_router
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