React-Router-dom'返回意外的对象

发布于 2025-01-26 01:39:41 字数 538 浏览 4 评论 0原文

注意:我正在尝试使用纱线切换到插件。

USEATICE是从“ React-Router-Dom”软件包导入的。

以前(在PNP设置之前)以下行:

const location = useLocation()

始终返回一个带有下一个结构(例如)的位置对象:

{pathname: '/', search: '', hash: '', state: null, key: 'default'}

使用当前的纱线PNP设置,在第一个渲染上构建渲染应用程序时,有一个带有上述结构的位置对象,但是在尝试更改路径时,有一个具有下一个结构(例如)的对象:

{action: 'PUSH', location: {pathname: '/new-path', search: '', hash: '', state: null, key: 'default'}}

为什么是这样,以及如何修复它(始终具有相同结构的Useleocation挂钩返回的位置对象)?

Note: I'm trying to switch to Plug'n'Play using yarn.

useLocation is imported from 'react-router-dom' package.

Previously (before pnp settings) the line below:

const location = useLocation()

always returned a location object with the next structure (e.g):

{pathname: '/', search: '', hash: '', state: null, key: 'default'}

With current yarn pnp settings, build renders app when on the first render, there is a location object with the structure as above, but when trying to change path, there is an object with the next structure (e.g.):

{action: 'PUSH', location: {pathname: '/new-path', search: '', hash: '', state: null, key: 'default'}}

Why is it so, and how to fix it (to always have a location object returned by useLocation hook of the same structure)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷迭香的记忆 2025-02-02 01:39:41

解决了。

问题是将历史程序包升级到V.5.x。同时,React-Router设置为v。^5.2.1。因此,历史v.5与React-Router V.5不兼容。

看起来历史v.5.5旨在为了即将到来的React-Router v.6。

将历史记录降低到第4节解决了问题。

注意:问题与切换到纱线插头无关。

Resolved.

The issue was that history package was upgraded to v.5.x. At the same time react-router is set to v.^5.2.1. So, the history v.5 is incompatible with react-router v.5.

Looks like history v.5.x is intended for coming react-router v.6.

Downgrading history to v.4 resolved the issue.

Note: the issue has nothing to do with switching to yarn Plug'n'Play.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文