Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS : In react-rooter-dom, is useHistory deprecated?

Using this code :

import { Link, useHistory } from "react-router-dom";

...

const history = useHistory();

...

history.push("/login");

This error shows up :

enter image description here

Is useHistory obsolete ? Or I do need to use another function ?

like image 254
Polymood Avatar asked Apr 25 '26 04:04

Polymood


1 Answers

Yes it is depreciated in V6 of react-router. In this version to navigate use the useNavigate hook. Check the docs here React Router V6 API Reference. Or more specifically the useNavigate hook here React Router v6 UseNavigate Hook. So like so:

import { useNavigate } from 'react-router-dom'

const navigate = useNavigate()

navigate('/login')
like image 163
Steve K Avatar answered Apr 26 '26 18:04

Steve K



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!