When building a React application, we often need to move users from one page to another.
For example:
After login, move the user to the Home page.
After submitting a form, move to the Success page.
After clicking a button, open the Cart page.
We can use useNavigate() to do this.
1. What is useNavigate()?
useNavigate() is a Hook provided by React Router.
It helps us move from one page to another using JavaScript.
In simple words, useNavigate() allows us to change pages when an action happens.
2. How to use useNavigate()
First, import useNavigate from React Router:
import { useNavigate } from "react-router-dom" ;
Enter fullscreen mode
Exit fullscreen mode
Inside your component, call it:
const navigate = useNavigate ( ) ;
Enter fullscreen mode
Exit fullscreen mode
Now, we can use navigate() to move to another page.
3.…