Home > Enterprise >  Control the URL bar with JavaScript
Control the URL bar with JavaScript

Time:09-26

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

CodePudding user response:

You can use useHistory hook:

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

Then :

const {location} = useHistory()
console.log(location.pathname)

CodePudding user response:

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

  • Related