我在使用 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
<DataGrid.Columns>
// 错误信息
import { useHistory } from "react-router-dom";
const history = useHistory();
useEffect(() => {
if (history.location.state) {
const taskId: any = history.location.state.taskId
getItemList(taskId)
}
}, [])
答案 0 :(得分:0)
我遇到了同样的错误,这让我有点困惑,我在任何地方都找不到答案。
在我的情况下,错误是由于接收状态的页面被分配了类型,而传入状态的类型是 any(所以它是未知的)。当您使用 typescript 并且它是强类型时,您需要在 useHistory() 调用中声明变量的类型,如下所示:
import { useHistory } from "react-router-dom";
const history = useHistory({
taskId: number;
});
useEffect(() => {
if (history.location.state) {
const taskId = history.location.state.taskId
getItemList(taskId)
}
}, [])