使用从根目录导航

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

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:

&lt;span onClick={() =&gt; navigate(`portfolio/${user}`)}
      className=&quot;text-orange-400 cursor-pointer&quot;
              &gt;
                {profile?.name}
              &lt;/span&gt;

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

huangapple
  • 本文由 发表于 2023年5月17日 17:19:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270473.html
匿名

发表评论

匿名网友

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

确定