首页 > 解决方案 > react-router-dom - TypeError:无法读取未定义的属性(读取“路径名”)

问题描述

在使用 react-router-dom 5.3.0 时,我正在打开一个模式但没有更改路由,并且出现此错误。

我已经将 react-router-dom 降级到 5.2.0 并且没有出现这个错误。

任何帮助深表感谢 :-)

错误信息

标签: reactjsreact-router-domantd

解决方案


升级 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;
}

推荐阅读