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

Viewed 915

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 ?

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')
Related