Navigate Programmatically

Learn how to navigate the user programmatically in a React application.

Introduction

In some cases, we’ll need to redirect the user to a specific URL when the user completes any action or task, such as submitting a form or creating an account.

To accomplish this, react-router provides two ways to navigate users programmatically:

  1. The useNavigate hook
  2. The Navigate element

1. Using the useNavigate hook

The useNavigate hook returns a function that we can use inside another function or in elements such as buttons to help navigate the user programmatically.

Use the coding widget given in the exercise to open the ContactDetails.js file and paste the following code:

Get hands-on with 1200+ tech skills courses.