ReactJS:在 react-rooter-dom 中,是否弃用了 useHistory?

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

使用此代码:

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

...

const history = useHistory();

...

history.push("/login");

出现此错误:

useHistory 过时了吗? 或者我确实需要使用其他功能?

是的,它在 react-router 的 V6 中已弃用。在此版本中导航使用 useNavigate 挂钩。在此处查看文档 React Router V6 API Reference. Or more specifically the useNavigate hook here React Router v6 UseNavigate Hook。像这样:

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

const navigate = useNavigate()

navigate('/login')