0

Is there some function in javascript or React.js to tell if a user tries to access an existing page through the URL bar?

  • Hi ! Your needs are not clear for me. Do you want to detect that the user is leaving a page, going into the omnibox ? OR, in case he types something in it, to check if this url is reachable ? – Philippe Sep 26 '21 at 14:52

2 Answers2

1

In plain javascript: window.history How do I modify the URL without reloading the page?

In React, use should use React Router along with useHistory hook : https://reactrouter.com/web/guides/quick-start

Kevin Gilbert
  • 643
  • 4
  • 20
0

You can use useHistory hook:

import {useHistory} from “react-router-dom”

Then :

const {location} = useHistory()
console.log(location.pathname)
Jad Alhamwi
  • 124
  • 1
  • 6