history object react router code example
Example 1: use history in react router
import {createBrowserHistory} from 'history';import {useRouterHistory, Router, Route} from 'react-router';const history = useRouterHistory(createBrowserHistory)({ basename: '/basename'});history.listen(location => { history.push('/super/url');});const router = ( <Router history={history}> <Route path=’/' component={App}> … </Route> </Router>);
Example 2: props history
class Comp extends React.Component {
componentDidUpdate(prevProps) {
const locationChanged =
this.props.location !== prevProps.location;
const locationChanged =
this.props.history.location !== prevProps.history.location;
}
}
<Route component={Comp} />;
Example 3: props history
{
key: 'ac3df4',
pathname: '/somewhere',
search: '?some=search-string',
hash: '#howdy',
state: {
[userDefined]: true
}
}