Unknown developer
Unknown developer

Reputation: 5920

React router 5: Know when there is a manual URL change

What I want is to prevent some routes from being triggered manually via the browser URL. So, is there any way to tell if the route to be applied has been triggered manually(typing in the browser URL location) or through React router (<Link> or useHistory().push)?

Upvotes: 1

Views: 799

Answers (1)

Chase
Chase

Reputation: 2304

When a user manually browses the entire app is remounted, when react router is used the route is "fake" and only what is new is mounted. You could set window.location to state in a component above your routing logic that is set on mount. This would stay the same when react router does the routing, but change when manually routing since the app would remount. You could examine this route for sub-paths to see if the user manually browsed.

I would suggest using protected routes instead. You could "enable" the routes when the conditions you need in your app are met.

Upvotes: 1

Related Questions