英文:
location path comes as undefined in react application
问题
以下是翻译好的部分:
我在React应用程序中有以下的`js`文件:
const Header = (props) => {
const pathName = props?.location?.pathName;
return (
//html
);
};
export default withRouter(Header);
这里,pathName
总是为未定义,因此导致页面重定向无法正常进行。
控制台中显示以下错误:
[![enter image description here][1]][1]
这是我的软件包版本:
"react": "^18.2.0",
"react-bootstrap": "^2.7.2",
"react-dom": "^18.2.0",
"react-router": "^6.8.1",
"react-router-dom": "^5.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
<details>
<summary>英文:</summary>
I am having below `js` file in react application
const Header = (props) => {
const pathName = props?.location?.pathName;
return (
//html
);
};
export default withRouter(Header);
Here `pathName` always comes as undefined, and because of which page redirection is not happening.
Console has below errors
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/AmwnL.png
Here is my package versions
"react": "^18.2.0",
"react-bootstrap": "^2.7.2",
"react-dom": "^18.2.0",
"react-router": "^6.8.1",
"react-router-dom": "^5.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
</details>
# 答案1
**得分**: 2
你应该使用 `location.pathname` 而不是 `location.pathName`。
<details>
<summary>英文:</summary>
You should be using `location.pathname` not `location.pathName`.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论