【发布时间】:2021-12-25 05:23:21
【问题描述】:
我想在单击按钮时转到另一个页面并尝试此代码,但出现此错误 “TypeError:Cannot read properties of undefined (reading ‘push’)”
我该如何解决这个问题? 谁能帮帮我?
import { useHistory } from 'react-router-dom';
function app() {
let history = useHistory();
const redirect = () => {
history.push('/pathOfMyPage ');
}
return (
<div>
<button onClick={redirect}>Click Here </button>
</div>
);
}
【问题讨论】:
-
你在用 react-router v6 吗?
-
cat package.json | grep react-router -
你能说明你是如何使用这个功能的吗?我觉得奇怪的是它是一个小写的名字。在 JSX 中可用的 React 组件必须是大写的。
标签: reactjs