【发布时间】:2020-10-18 05:13:36
【问题描述】:
我有这个。 它与文档中所说的完全相同。 我认为 react-router-dom 模块很好,因为在其他组件中 BrowserRouter、Router 和 Link 对我有用
import { useHistory } from "react-router-dom"
import React from 'react'
export default function HomeButton() {
let history = useHistory()
function handleClick() {
history.push("/home")
}
return (
<button type="button" onClick={handleClick}>
Go home
</button>
);
}
当我点击按钮时会发生这种情况
TypeError: 无法读取未定义的属性“push”
我是reactjs的新手,请帮忙,谢谢
【问题讨论】:
-
useHistory无法在您拥有 Routes 的组件中工作,因为尚未设置 useHistory 所需的上下文。useHistory将适用于您在您中声明的任何子组件或组件,但不适用于 的父组件或组件本身
标签: reactjs react-hooks react-router-dom