0
点赞
收藏
分享

微信扫一扫

Login.js:33 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘push‘)


React项目在进行路由跳转的时候,使用

props.history.push('/index');

会报错

Login.js:33 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘push‘)_react.js

原因:​​react-router-dom​​​最新版本中,使用​​useNavigate​​进行编程式跳转

使用useNavigate钩子函数生成navigate对象,可以通过JS代码完成路由跳转


useNavigate取代了原先版本中的useHistory


解决

使用以下代码进行路由跳转

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

function Foo(){
const navigate = useNavigate();
return (
<div onClick={() => navigate('/foo')}>跳转</div>
)
}



举报

相关推荐

0 条评论