英文:
useNavigate from root directory
问题
I'm trying to redirect a user after they click on a text with useNavigate
, the route that I'm trying to use is "behind" the current route whereas useNavigate
redirects "forward".
code:
<span onClick={() => navigate(`portfolio/${user}`)} className="text-orange-400 cursor-pointer">
{profile?.name}
</span>
current route is http://localhost:3000/packages/PUeV5ZTXMfTRFIsoYyKi
and the route is redirected to is http://localhost:3000/packages/PUeV5ZTXMfTRFIsoYyKi/portfolio/ZLPxfKnsf9sYo3l9QljPD9dfq8qM
instead of http://localhost:3000/portfolio/PUeV5ZTXMfTRFIsoYyKi
.
I've also tried onClick={() => navigate(
portfolio/${user}, { replace: true })}
but the redirect is the same.
英文:
I'm trying to redirect a user after they click on a text with useNavigate
, the route that i'm trying to use is "behind" the current route whereas useNavigate
redirects "forward".
code:
<span onClick={() => navigate(`portfolio/${user}`)}
className="text-orange-400 cursor-pointer"
>
{profile?.name}
</span>
current route is http://localhost:3000/packages/PUeV5ZTXMfTRFIsoYyKi
and the route is redirected to is http://localhost:3000/packages/PUeV5ZTXMfTRFIsoYyKi/portfolio/ZLPxfKnsf9sYo3l9QljPD9dfq8qM
instead of http://localhost:3000/portfolio/PUeV5ZTXMfTRFIsoYyKi
.
I've also tried onClick={() => navigate(`portfolio/${user}`, { replace: true })}
but the redirect is the same.
答案1
得分: 1
<span onClick={() => navigate(/portfolio/${user}
)}
...
英文:
<span onClick={() => navigate(/portfolio/${user}
)}
...
</span>
答案2
得分: 0
因为这是一个相对路径。
要导航到绝对路径,您需要以 /
开头。
navigate(`/portfolio/${user}`)`
英文:
because this is a relative route.
To navigate to an absolute route you need to start with /
navigate(`/portfolio/${user}`
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论