Menu

useNavigate() in React Router
📰

useNavigate() in React Router

DEV Community: react·Lokesh·about 2 hours ago
#eWcXj9M6
#devtoreact#devto#dev#navigate#usenavigate
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.