Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'react-router-dom' does not contain an export named 'useHistory'

Im trying to import useHistory from react-router-dom but it keeps giving me the error 'react-router-dom' does not contain an export named 'useHistory'.

React-router-dom's version is 4.3.1 and i've tried updating (in case there was an updated version) but every time i do npm install react-router-dom it always just installs 4.3.1 It updated react-router from 4.3.1 to 5.2.0 just fine so i don't know why react-router-dom doesn't update (if there is an updated version).

If anyone knows another way to import useHistory or make a back button that goes to the previous URL from anywhere I'd love to know.

like image 471
Stacklep1 Avatar asked Feb 02 '26 01:02

Stacklep1


2 Answers

"useHistory" is replaced by "useNavigate", in "react-router-dom" v.6

import { useNavigate } from 'react-router-dom';
const navigateTo = useNavigate();
navigateTo('/your-page')
like image 55
Emad Armoun Avatar answered Feb 03 '26 14:02

Emad Armoun


You can update react-router-dom package by manually changing the version in package.json to 5.2.0

or

install the specific version by using the below command

npm i [email protected]

or the latest version like

npm i react-router-dom@latest
like image 24
Shubham Khatri Avatar answered Feb 03 '26 16:02

Shubham Khatri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!