I'm using hooks and functional components in all my react project and don't have any class component. So i want a hook to use for routing (so don't want to use react-router library). I googled and found some hooks and choose the hookrouter. Since the app component got re-rendered while route changes, so can't use Context to save some data along with the project. I want a routing hook that is compatible with react-context (context-provider and useContext of course). Do any one knows one? Thanks for spending your time.
Asked
Active
Viewed 349 times
1
-
The may help you :) https://medium.com/@Charles_Stover/how-to-convert-withrouter-to-a-react-hook-19bb02a29ed6 – Bhojendra Rauniyar Oct 20 '19 at 07:28
-
@BhojendraRauniyar Thank you so much, but i studied it before. The problem is it is not as simple to implement as i want – Amin Oct 20 '19 at 08:25
1 Answers
0
Use useHistory hook to navigate. This hook is compatible with router dom version 5.1.0 and above.
import { useHistory } from 'react-router-dom'
const history = useHistory();
history.push({pathname:'/home'});

Hasan Zubairi
- 1,037
- 4
- 23
- 57
-
1Thank you very much but just now i'm using `useHistory`. At the time i was writing this question, this hooks were not exist ;) – Amin Jul 07 '20 at 09:41