Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

useLocation hook - determine past locations

According to React Router 5.1 documentation it should be possible to see "where the app is now, where you want it to go, or even where it was". In my app I need to see "where it was" - what locations I have visited before landing at a specific location.

More precisely; I wish to find a previous location matching a certain pattern. That location might be two or three locations ago.

However - I cannot figure out how to perform this.

What is a recommended approach to achieve this?

like image 689
Kermit Avatar asked Sep 15 '25 18:09

Kermit


1 Answers

I turns out the best way, for me, to see where the application has been been is to simply use the state property in the React Router Link.

This article on how to pass state from Link to components really helped explain how to use the Link state.

Basically the Link can pass the state property to the rendered component.

<Link to={{ pathname: "/courses", state: { fromDashboard: true } }} />

The rendered component then access the state via props.location.state

This in conjunction with passing props to components generating the links solved my problem!

like image 180
Kermit Avatar answered Sep 17 '25 08:09

Kermit