How do I navigate to previous page in React using useNavigate

0 votes
Can you tell me with the help of an example and code how I navigate to the previous page in React using useNavigate?
Mar 7 in Angular by Nidhi
• 12,580 points
71 views

1 answer to this question.

0 votes

In React, you can navigate to the previous page using the useNavigate hook from React Router v6. The useNavigate hook provides a navigate function that allows you to programmatically navigate. To go back to the previous page, you can pass -1 as an argument to the navigate function.

Steps:

Import useNavigate from react-router-dom.

Call useNavigate to get the navigate function.

Use navigate(-1) to go back to the previous page.

Example Code:

import React from 'react';

import { useNavigate } from 'react-router-dom';

function BackButton() {

  const navigate = useNavigate();

  const goBack = () => {

    navigate(-1); // Navigates to the previous page

  };

  return (

    <button onClick={goBack}>Go Back</button>

  );

}

export default BackButton;

answered Mar 7 by Tanvi

Related Questions In Angular

0 votes
1 answer

How do I conditionally add attributes to React components?

Hello @kartik, For certain attributes, React is intelligent ...READ MORE

answered Jul 22, 2020 in Angular by Niroj
• 82,840 points
9,003 views
0 votes
1 answer

How to get last visited page URL in React?

To get the last visited page URL ...READ MORE

answered Mar 6 in Angular by Anvi
56 views
0 votes
1 answer

How can we go back to previous page after having some error on request made through current page ?

$route is used for deep-linking URLs to controllers ...READ MORE

answered Feb 11, 2020 in Angular by Niroj
• 82,840 points
1,467 views
0 votes
1 answer

How do you import a javascript package from a cdn/script tag in React?

Hello, Go to the index.html file and import ...READ MORE

answered Jun 4, 2020 in Angular by Niroj
• 82,840 points
25,389 views
0 votes
0 answers

What is the best way to share services across Modules in angular2?

i want know with the help of ...READ MORE

Mar 5 in Angular by Nidhi
• 12,580 points
69 views
0 votes
0 answers

How do you use forkJoin() for parallel API calls?

can you explain me with the help ...READ MORE

Mar 5 in Angular by Nidhi
• 12,580 points
37 views
0 votes
0 answers

What are the differences between mergeMap, concatMap, and switchMap?

can someone explain me What are the ...READ MORE

Mar 5 in Angular by Nidhi
• 12,580 points
84 views
0 votes
0 answers

How does takeUntil() help in cleaning up Observables?

i want know with the help of ...READ MORE

Mar 5 in Angular by Nidhi
• 12,580 points
76 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP