56

I have this paths in react-router-dom:

 <BrowserRouter>
  <div>
  <Route exact path='/(index.html)?' component={Home}/> {/* app = home */}
  <Route  path='/register' component={Registration}/>
  </div>
</BrowserRouter>

everything is working fine, now anywhere in my components I want to change path by onClick, a code like this:

<button onClick={() => history.push('/the/path') }> change path </button>

how can I do that?

gpbaculio
  • 5,693
  • 13
  • 60
  • 102

6 Answers6

59
import {withRouter} from 'react-router-dom';
...

class App extends React.Component {
  ...

  nextPath(path) {
    this.props.history.push(path);
  }

  render() {
    return (
      <button onClick={() => this.nextPath('/the/path') }>
        change path 
      </button>
    );
  }
}

export default withRouter(App);
soywod
  • 4,377
  • 3
  • 26
  • 47
56

If you're using the button only for navigation, you can replace it with <Link />1 and apply a button style.

<Link to='/new/location/'>Click Me</Link>

Or you can use the <NavLink />2.

In case of using Material UI, you can use the following code:

import { Link } from 'react-router-dom'
import Button from '@material-ui/core/Button';

<Button component={Link} to="/new/location/">
  Click Me
</Button>

(1): import {Link} from "react-router-dom";
(2): import {NavLink} from "react-router-dom";

frogatto
  • 28,539
  • 11
  • 83
  • 129
45

react-router-dom exports a hook called useHistory. Just import it and use it in your component like this:

import React from 'react';
import { useHistory } from 'react-router-dom';

export default () => {
  const history = useHistory();
   
  return (
    <button onClick={() => history.push('/your/path')}>
      Click me
    </button>
  );
};

I'm using:

  • "react": "^16.13.1"
  • "react-router-dom": "^5.2.0"

Check this post for more details: https://ultimatecourses.com/blog/programmatically-navigate-react-router

Bruno Ribeiro
  • 646
  • 6
  • 13
30

in react-router-dom version 6 use useNavigate

import { useNavigate } from "react-router-dom";
import { Button } from "@mui/material";
...
const navigate = useNavigate();
<Button
   onClick={() => navigate("/your-path-here")}>
click me
</Button>

do not forget to install from mui.com to use the <Button></Button> component

Abey Bruck
  • 532
  • 5
  • 7
1

in react-router-dom version 6, you can also use the Link component too. This is to back @Abey Bruck's answer

import { Button } from "@chakra-ui/react";
...

const Home = () => {
  return (
    <Button as={Link} to={'/login'}>Login Page</Button>
  )
}
AbdulAzeez Olanrewaju
  • 976
  • 1
  • 13
  • 32
1
import { Button } from "@mui/material";
const navigate = useNavigate();
  const submitHandler = (e) => {
    e.preventDefault();
    navigate(`/`);
  };
<Button variant="contained" onClick={submitHandler}>Home Page</Button>
  • you should add some documentation on the `useNavigate` hook and a link so that the user can check docs and learn than just code – Saravanan Sep 18 '22 at 10:18