Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created February 19, 2020 17:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sibelius/7d760fa16a121ec0f4cd8b143a59bee6 to your computer and use it in GitHub Desktop.
Save sibelius/7d760fa16a121ec0f4cd8b143a59bee6 to your computer and use it in GitHub Desktop.
withRouter for react-router-dom v6
import { useHistory } from 'react-router-dom';
export const withRouter = (Component) => {
const Wrapper = (props) => {
const history = useHistory();
return (
<Component
history={history}
{...props}
/>
);
};
return Wrapper;
};
@MuhammadZaeemNaveedSheikh

use history also not working

@Abinashshasini
Copy link

useHistory is replaced with useNavigate

@RyanSpeciale
Copy link

wait so useNavigate provides the same functionality as withRouter?

@sahilbarvaliya
Copy link

useNavigate working perfectly...

@SateeshKumbhakar
Copy link

hello i am using v6 and I want to redirect (navigate) on the /dashboard how do I use useNavigate in action.
return navigate('/dashboard') <Navigate to='/dashboard' />

@RaduRS
Copy link

RaduRS commented Feb 2, 2022

  <button onClick={() => {navigate("/dashboard");}} >
        Dashboard
  </button>

@SateeshKumbhakar
Copy link

I tried this, Thank you @RaduRS

@PavelHasala
Copy link

Can somebody help me, how to type the Component parameter, so it is not any, when passing class components?

Copy link

ghost commented Jun 14, 2022

useNavigate working perfectly...

Okay Bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment