React Router V6总是渲染“//&quot”
我正在尝试在React-Create-App中实现路由器,但它总是呈现“/”并显示Home或Signin页面。我该如何解决?
function AppRouter({ isLoggedIn, user }) {
return(
<Router>
<Routes>
<Route path="/profile" element={<Profile />} />
<Route path="/signUp" element={<SignUp />} />
{isLoggedIn
? <Route exact path={"/"} element={<Home user={user}/>} />
: <Route exact path={"/"} element={<SignIn />} />
}
</Routes>
</Router>
)
}
I'm trying to implement router in react-create-app but it always render "/" and showing Home or SignIn page. How can I solve this?
function AppRouter({ isLoggedIn, user }) {
return(
<Router>
<Routes>
<Route path="/profile" element={<Profile />} />
<Route path="/signUp" element={<SignUp />} />
{isLoggedIn
? <Route exact path={"/"} element={<Home user={user}/>} />
: <Route exact path={"/"} element={<SignIn />} />
}
</Routes>
</Router>
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您对
hashrouter
与UI的工作方式有轻微的误解。hashrouter
处理带有URL哈希值的路由,即在URL中的所有内容“#”
。如果您试图渲染应用程序并访问“&lt; domain&gt;/“
,而不是”&lt; domain&gt;/#/“/#/”
路由无法正常工作。例如,在您的运行代码和盒子演示中,基本URL为
“ https://5p7hff.csb.app/”
。在此基础网址上,哈希路由器无法真正工作,您应该真正访问“ https://5p7hff.csb.app.app/#/”
而不是加载哈希路由器,并加载了应用程序。内部路由可以起作用。来自
“ https://5p7hff.csb.app/#/”
您应该导航到任何路由,即 ”/profile“ 和
https://5p7hff.csb.app/#/signup ?fontsize = 14&amp; hidenAvigation = 1&amp; inditialpath =%2f%23%2F&amp; module =%2FSRC%2fapp.js&amp; themp; them = dark“ rel =“ rel =“ nofollow noreferrer” /static/img/play-codesandbox.svg“ alt =”编辑Yound-Dawn-ejyruw“>
如果您切换到其他路由器,例如
browserrouter
the,则
” /#/“
不再使用,路由器和路由从”/“”
渲染。应用程序从哪里运行。路由将为“ https:// 5p7hff。 csb.app/“
,” https://5p7hff.csb.app/profile“
和” https://5p7hff.csb.app/signup”代码>。
It seems you have a slight misunderstanding of how the
HashRouter
works with the UI.The
HashRouter
handles routing with a URL hash value, i.e. everything after the"#"
in the URL. If you are trying to render your app and access"<domain>/"
instead of"<domain>/#/"
the routing won't work.For example in your running codesandbox demo, the base URL is
"https://5p7hff.csb.app/"
. At this base URL the hash router isn't really working, and you should really be accessing"https://5p7hff.csb.app/#/"
instead so the hash router is loaded and the app's internal routing can work.From
"https://5p7hff.csb.app/#/"
you should be to then navigate to any of your routes, i.e."https://5p7hff.csb.app/#/profile"
andhttps://5p7hff.csb.app/#/signUp"
.If you switch to a different router, like the
BrowserRouter
then the"/#/"
is no longer used, the router and routes render from"/"
where the app is running from. The routes would be"https://5p7hff.csb.app/"
,"https://5p7hff.csb.app/profile"
, and"https://5p7hff.csb.app/signUp"
.