1import { useHistory } from "react-router-dom";
2
3function HomeButton() {
4 let history = useHistory();
5
6 function handleClick() {
7 history.push("/home");
8 }
9
10 return (
11 <Button type="button" onClick={handleClick}>
12 Go home
13 </Button>
14 );
15}
1window.history.pushState("http://example.ca", "Sample Title", "/example/path.html");
2
1// usually all you need
2<Link to="/somewhere"/>
3
4// but you can use a location instead
5const location = {
6 pathname: '/somewhere',
7 state: { fromDashboard: true }
8}
9
10<Link to={location}/>
11<Redirect to={location}/>
12history.push(location)
13history.replace(location)
14
1class Comp extends React.Component {
2 componentDidUpdate(prevProps) {
3 // will be true
4 const locationChanged =
5 this.props.location !== prevProps.location;
6
7 // INCORRECT, will *always* be false because history is mutable.
8 const locationChanged =
9 this.props.history.location !== prevProps.history.location;
10 }
11}
12
13<Route component={Comp} />;
14
1{
2 key: 'ac3df4', // not with HashHistory!
3 pathname: '/somewhere',
4 search: '?some=search-string',
5 hash: '#howdy',
6 state: {
7 [userDefined]: true
8 }
9}
10