1import { useHistory } from 'react-router-dom';
2
3function Home() {
4 const history = useHistory();
5 return <button onClick={() => history.push('/profile')}>Profile</button>;
6}
1import React from "react";
2import ReactDOM from "react-dom";
3import {
4 BrowserRouter as Router,
5 Switch,
6 useLocation
7} from "react-router-dom";
8
9function usePageViews() {
10 let location = useLocation();
11 React.useEffect(() => {
12 ga.send(["pageview", location.pathname]);
13 }, [location]);
14}
15
16function App() {
17 usePageViews();
18 return <Switch>...</Switch>;
19}
20
21ReactDOM.render(
22 <Router>
23 <App />
24 </Router>,
25 node
26);
27
1In react-router-dom version 6
2useHistory() is replaced by useNavigate() ;
3
4import {useNavigate} from 'react-router-dom';
5const navigate = useNavigate();
6navigate('/home')