React-Router-dom'返回意外的对象
注意:我正在尝试使用纱线切换到插件。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了。
问题是将历史程序包升级到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.