Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created February 19, 2020 17:08
Show Gist options
  • Select an option

  • Save sibelius/7d760fa16a121ec0f4cd8b143a59bee6 to your computer and use it in GitHub Desktop.

Select an option

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
Copy link
Copy Markdown

use history also not working

@Abinashshasini
Copy link
Copy Markdown

useHistory is replaced with useNavigate

@RyanSpeciale
Copy link
Copy Markdown

wait so useNavigate provides the same functionality as withRouter?

@sahilbarvaliya
Copy link
Copy Markdown

useNavigate working perfectly...

@SateeshKumbhakar
Copy link
Copy Markdown

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
Copy Markdown

RaduRS commented Feb 2, 2022

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

@SateeshKumbhakar
Copy link
Copy Markdown

I tried this, Thank you @RaduRS

@PavelHasala
Copy link
Copy Markdown

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

Copy link
Copy Markdown

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