react-router: export 'useHistory' was not found in 'react-router-dom'
Version
“react”: “^16.8.6”, “react-router”: “^6.0.0-alpha.2”, “react-router-dom”: “^6.0.0-alpha.2”
Steps to reproduce
Try to use history hook in a React function component:
import { useHistory } from 'react-router-dom';
…
const history = useHistory();
Expected Behavior
History should work as expected.
Actual Behavior
Getting the error:
export 'useHistory' was not found in 'react-router-dom'
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 5
- Comments: 17 (1 by maintainers)
Using Typescript, I fixed this error by updating the types definition to v5… npm install -save-dev @types/react-router-dom
There is no
useHistoryhook in 6.0. Switch back to 5.1.2 or adjust your code to not use that hook.I use this in V6 and it works import {useNavigate } from ‘react-router-dom’
const navigate = useNavigate() // inside the function
navigate(
/section/)In react-router-dom V6, you should switch from useHistory to useNavigate.
So in v5, you can have: import { useHistory } from “react-router-dom”; let history = useHistory(); history.push(“/home”);
But in v6, you have to switch it to : import { useNavigate } from “react-router-dom”; let navigate = useNavigate(); navigate(“/home”);
It solve my problem
Same here.
useNavigate thing is working fine
@timdorr I updated react-router-dom to 5.1.2 still i am getting error …/@types/react-router-dom has no exported member ‘useHistory’