useHistory() 在 react-router-dom 中的块方法返回与预期不同的类型



我突然发现了这个问题,这让我很困惑。我正在使用react router dom并访问useHistory挂钩。

const history = useHistory();
history.block((tx) => {
console.log(tx)
return true;
})

"tx";应该是一种";转换<H.位置状态>";,其具有";动作";以及";位置";属性。然而,控制台日志记录";tx";显示tx如下:

{pathname: "/memberships/add", search: "", hash: "", state: undefined, key: "1wlr2w"}

这似乎是";tx";。然而,当我使用TypeScript时,这会阻止我访问";tx";直接地根据文档,这应该在我上面的例子中起作用,因为阻塞函数在更改路由时会正确激活(每次都调用console.log,不会发生错误(。我可以解决这个问题,但我想知道这里的根本问题是什么,或者我只是从根本上误解了什么。

谢谢你的帮助。

试试这种方法。在你的情况下,tx只有位置信息。

const history = useHistory();
history.block((location, action) => {
console.log(location, action)
return true;
})