August 26, 2022

Mutiple ways to programmatically navigate using React router

 Mutliple ways to navigate between pages in react using react router. 

  • React Router v6
            useNavigate hook can be used.
 
  import { useNavigate } from "react-router-dom"
  const Component = () => {
      let navigate = useNavigate();
      navigate("/pathname", state: { someData }); 
  }

  • React Router v5
            useHistory hook can be used.
 
  import { useHistory } from "react-router-dom"
  const Component = () => {
      let history = useHistory();
      history.push("/pathname"); 
  }


No comments:

Post a Comment