【发布时间】:2021-07-13 20:35:24
【问题描述】:
我在使用 react hook useHisotry 时遇到错误Object is of type 'unknown',这是详细信息,提前感谢您的帮助!
节点版本:v12.14.1
反应版本:17.0.1
react-router-dom 版本:16.9.8
@types of react-router-dome 版本:^5.1.6
import { useHistory } from "react-router-dom";
const history = useHistory();
useEffect(() => {
if (history.location.state) {
const taskId: any = history.location.state.taskId
getItemList(taskId)
}
}, [])
// 错误信息
Object is of type 'unknown'. TS2571
252 | useEffect(() => {
253 | if (history.location.state) {
> 254 | const taskId: any = history.location.state.taskId
| ^
257 | }
【问题讨论】:
-
为什么要这样声明常量
const taskId: any = history.location.state.taskId? -
请只尝试这样
const taskId=history.location.taskId -
@EtsukoSusui 你确定吗?你知道那里有什么吗?
-
您是否在组件实现之外使用历史记录?
-
@lissettdm,不,我在函数组件中使用它。
标签: reactjs react-router react-hooks react-router-dom