reactjs - react-router-dom - TypeError:无法读取未定义的属性(读取“路径名”)
问题描述
在使用 react-router-dom 5.3.0 时,我正在打开一个模式但没有更改路由,并且出现此错误。
我已经将 react-router-dom 降级到 5.2.0 并且没有出现这个错误。
任何帮助深表感谢 :-)
解决方案
升级 react-router-dom 版本后,当我尝试使用location.pathname
.
使用useLocation()
钩子解决了我的问题:
import { useLocation } from 'react-router-dom';
// getting current pathname with custom hook
const useName = () => {
const location = useLocation();
return location.pathname;
}
推荐阅读
- python - 如何使用 tensor.item() ?IndexError:0-dim 张量的无效索引。使用 tensor.item() 将 0-dim 张量转换为 Python 数字
- python - 如何获取连接到我家 wifi 的所有计算机的列表
- kotlin - Kotlin 中的深度复制
- jpa - 如何比较criteriaBuilder.greaterThan()中的日期?
- mysql - MySQL-每分钟获取记录数
- python - 获取跨运行稳定的函数的散列
- matlab - matlab浮点数精度2^(-45)
- powershell - PowerShell 不断在我的重命名中添加句点
- c# - 有没有办法按特定顺序跟踪 vuforia 目标?
- angular - 过滤数组 A 中 id 出现在数组 B 中的所有对象