React路由器DOM V6
导航
组件和从
usenavigate()
挂钩?
if (therapy !== desiredGroupTherapy || required.includes('admin') && !isAdmin) {
const pathname = generatePath(pageRoutes.DASHBOARD, {
groupId: desiredGroupId,
therapy: therapyForValidGroup,
})
navigate(pathname, { replace: true })
// return <Navigate to={pathname} replace />
}
我在此处对导航
有问题。应该如何工作:我将其重定向到该页面,但是第一次没有治疗
,因此我应该重定向到同一页面,但是我的Therapy
现在将等于治疗ForvalidGroup
。我有自定义挂钩Usethapy()
,从URL接受Therapy
。因此,当它不确定时,它会使用navigate
函数崩溃。但是使用导航
组件,它可以正常工作。那么有什么区别呢?
What is the difference between Navigate
component and navigate
from useNavigate()
hook?.
if (therapy !== desiredGroupTherapy || required.includes('admin') && !isAdmin) {
const pathname = generatePath(pageRoutes.DASHBOARD, {
groupId: desiredGroupId,
therapy: therapyForValidGroup,
})
navigate(pathname, { replace: true })
// return <Navigate to={pathname} replace />
}
I have issue with navigate
here. How that should work: I redirect to that page, but at first time I have no therapy
, so I should redirect to the same page but my therapy
now will be equal to therapyForValidGroup
. I have custom hook useTherapy()
which takes therapy
from URL. So at first time when it is undefined it crashes using navigate
function. But using Navigate
component, it works fine. So what is the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
导航
功能(调用)是副作用。该代码直接在组件正文中调用作为无意的副作用。
将逻辑移至
使用效果
钩子。例子:
The
navigate
function, when invoked, is a side-effect. The code is callingnavigate
directly in the body of the component as an unintentional side-effect.Move the logic into a
useEffect
hook.Example: