ReactJs Redirect to URL


JavaScript navigator location.href property is used to redirect to specified url. Here in this tutorial, we are going to explain how you can use loccation.href in ReactJs. You can also use our online editor to edit and run the code online.


ReactJs Redirect to URL Method Example

You can redirect to specified url simply as below-

Example:

<script>
function MyFunction() {
  const currUrl = "Current Url = "+window.location.href;  
  return (    
  <div>   
    <p>Click To go www.google.com</p>
    <button onClick={TestFunction} style={{background:"skyblue", padding:"10px", margin:"0 auto"}}>
        Click Me
     </button>
    </div> 
  );
}
function TestFunction() {
  location.href = "https://www.google.com";
}

const myData = '';
ReactDOM.render(
  <MyFunction />,
  document.getElementById('root')
);
</script>

Try it »


Advertisements

Add Comment

📖 Read More