How to get last visited page URL in React

0 votes
I want the solution to this question How to get the last visited page URL in React?
Mar 6 in Angular by Nidhi
• 12,580 points
57 views

1 answer to this question.

0 votes

To get the last visited page URL in React, you can use the useNavigate and useLocation hooks (for React Router v6). Here's how to implement it:

For React Router v6:


import React, { useEffect, useRef } from 'react';

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

function MyComponent() {

  const location = useLocation();

  const lastVisitedUrl = useRef('');

  useEffect(() => {

    console.log('Last visited URL:', lastVisitedUrl.current);

    lastVisitedUrl.current = location.pathname; // Update the last visited URL

  }, [location]);


  return <div>Check the console for the last visited URL.</div>;

}

export default MyComponent;

answered Mar 6 by Anvi

Related Questions In Angular

0 votes
1 answer
0 votes
1 answer

How to update nested state properties in React

Hello @kartik, In order to setState for a nested object ...READ MORE

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

How to create a URL in the controller .NET MVC?

Hello @kartik, If you just want to get ...READ MORE

answered Jul 23, 2020 in Angular by Niroj
• 82,840 points
6,384 views
0 votes
1 answer

How to set a default base URL for all API calls in Angular?

In Angular, you can set a default ...READ MORE

answered Feb 26 in Angular by Kavya
68 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to create a mat table in Angular?

1. Install Angular Material ng add @angular/material 2. Import ...READ MORE

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

Cannot find name webgl object TypeScript issue TS2304.

The error Cannot find name 'WebGLObject'. TS2304 ...READ MORE

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

How can we reload a page after requesting the same get parameter using AngularJS?

hey kartik, The reason for this is that ...READ MORE

answered Feb 10, 2020 in Angular by Niroj
• 82,840 points
12,781 views
0 votes
1 answer

How to change the value of an Observable in TypeScript Angular?

To change the value of an Observable ...READ MORE

answered Feb 21 in Angular by Kavya
133 views
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