Lets say we create a form for a login page and when we clicked the div element to sign up, we want to redirect our page to "/createuser" page.
- We can import the useHistory hook from react-router-dom:
- We can declare the useHistory in a const variable:
- We add an event listener to our div, so that it can listen for a click and then passed in a callback function and then push the url route we want our form to redirect to once we click the sign up div to the useHistory that we declared inside the navigation variable.
Note that the useHistory() is for react-router-dom@5
If you are using the newer version then instead of using the useHistory Hook you can use the useNavigate Hook.
for example:
For the V5
const navigate = usehistory();
navigate.push("/");
For the V6
const navigate = useNavigate();
navigate("/")
Top comments (0)