ReactJs get Current Url
We can use native javascript window.location.href property to get the current url anywhere like component in react js. Here in this tutorial, we are going to explain how you can use this method to get the current url in reactjs. You can also use our online editor to edit and run the code online.
ReactJs get Current Url JavaScript Example
You can use window.location.href in react js simply as below-
Example:
<script type="text/es2015"> function MyFunction(props) { const currUrl = "Current Url = "+window.location.href; return ( <p>{currUrl}</p> ); } const myData = ''; ReactDOM.render( <MyFunction />, document.getElementById('root') ); </script> |
Output of above example-
Advertisements