In this example, We have shared how to navigate using react router programmatically? There is a new useHistory hook in React Router >5.1.0 if you are using React >16.8.0 and functional components.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import { useHistory } from "react-router-dom"; function HomeButton() { const history = useHistory(); function handleClick() { history.push("/home"); } return ( <button type="button" onClick={handleClick}> Go home </button> ); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.