位置路径在React应用程序中出现为未定义。

huangapple go评论61阅读模式
英文:

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) =&gt; {
             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

    &quot;react&quot;: &quot;^18.2.0&quot;,
    &quot;react-bootstrap&quot;: &quot;^2.7.2&quot;,
    &quot;react-dom&quot;: &quot;^18.2.0&quot;,
    &quot;react-router&quot;: &quot;^6.8.1&quot;,
    &quot;react-router-dom&quot;: &quot;^5.2.0&quot;,
    &quot;react-scripts&quot;: &quot;5.0.1&quot;,
    &quot;web-vitals&quot;: &quot;^2.1.4&quot;



</details>


# 答案1
**得分**: 2

你应该使用 `location.pathname` 而不是 `location.pathName`。

<details>
<summary>英文:</summary>

You should be using `location.pathname` not `location.pathName`.

</details>



huangapple
  • 本文由 发表于 2023年2月18日 20:24:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定