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)

Most upvoted comments

Using Typescript, I fixed this error by updating the types definition to v5… npm install -save-dev @types/react-router-dom

There is no useHistory hook 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”);

I use this in V6 and it works import {useNavigate } from ‘react-router-dom’

const navigate = useNavigate() // inside the function

navigate(/section/)

It solve my problem

I’m using react-router-dom 5.2.0 and got this error as well… And when it imports i cant use its properties, like push

TypeError: Cannot read property 'push' of undefined

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’