Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to programmatically navigate using React Router and JavaScript?

To Programmatically Navigate using React Router and JavaScript, we can use the useHistory hook.

For instance, we write

import { useHistory } from "react-router-dom";

const HomeButton = () => {
  const history = useHistory();

  const handleClick = () => {
    history.push("/home");
  };

  return (
    
  );
};

to call the useHistory hook to return the history object.

Then we define the handleClick function that calls history.push to navigate to the /home page.

And then we set the onClick prop toi handleClick to call handleClick when we click oin the button.

The post How to programmatically navigate using React Router and JavaScript? appeared first on The Web Dev.



This post first appeared on The Web Dev, please read the originial post: here

Share the post

How to programmatically navigate using React Router and JavaScript?

×

Subscribe to The Web Dev

Get updates delivered right to your inbox!

Thank you for your subscription

×