无法有效构建React Router结构

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

Unable to structure React Router efficiently

问题

我正在开发一个ReactJS项目,其中我使用了React Router。我有多个组件需要渲染,但不确定如何组织它。我没有得到预期的结果。

组件

  • Authentication(身份验证)
  • SideBar(侧边栏)
  • List(列表)
  • Detail(详情)

App.js(应用程序主文件)

  1. import .....
  2. .... //一些导入语句
  3. function App(){
  4. <BrowserRouter>
  5. <SideBar/>
  6. <Routes>
  7. <Route path="/" element={<List/>}/>
  8. <Route path="/detail" element={<Detail/>}/>
  9. <Route path="/authentication" element={<Authentication/>}/>
  10. </Routes>
  11. </BrowserRouter>
  12. }

在这里,我有4个组件。侧边栏组件将在DetailList组件中共享,但我不希望在Authentication组件中可见。

尝试使用Outlet

App.js(应用程序主文件)

  1. import .....
  2. .... //一些导入语句
  3. function App(){
  4. <BrowserRouter>
  5. <Routes>
  6. <Route path="/" element={<Authentication/>}/>
  7. <Route path="/app" element={<Container/>}/>
  8. </Routes>
  9. </BrowserRouter>
  10. }

Container.js(容器组件)

  1. import .....
  2. .... //一些导入语句
  3. function Container(){
  4. <SideBar/>
  5. <Routes>
  6. <Route path="/list" element={<List/>}/>
  7. <Route path="/detail" element={<Detail/>}/>
  8. </Routes>
  9. }

现在的问题是,当我导航到http://<域名>/app时,它显示一个带有侧边栏的空白页面,这没有实际用途。

如何实现这一目标?

英文:

I am working on a reactjs project where I utilize react-router. I have multiple components to render, but not sure how to structure it. I am not getting expected results

Components

  • Authentication
  • SideBar
  • List
  • Detail

App.js

  1. import .....
  2. .... //some import statements
  3. function App(){
  4. &lt;BrowserRouter&gt;
  5. &lt;SideBar/&gt;
  6. &lt;Routes&gt;
  7. &lt;Route path=&quot;/&quot; element={&lt;List/&gt;}/&gt;
  8. &lt;Route path=&quot;/detail&quot; element={&lt;Detail/&gt;}/&gt;
  9. &lt;Route path=&quot;/authentication&quot; element={&lt;Authentication/&gt;}/&gt;
  10. &lt;/Routes&gt;
  11. &lt;/BrowserRouter&gt;
  12. }

Here I have totally 4 components. The sidebar component will be common for both Detail and List component, & I don't want it to be visible for Authentication component.

Tried using the Outlet

App.js

  1. import .....
  2. .... //some import statements
  3. function App(){
  4. &lt;BrowserRouter&gt;
  5. &lt;Routes&gt;
  6. &lt;Route path=&quot;/&quot; element={&lt;Authentication/&gt;}/&gt;
  7. &lt;Route path=&quot;/app&quot; element={&lt;Container/&gt;}/&gt;
  8. &lt;/Routes&gt;
  9. &lt;/BrowserRouter&gt;
  10. }

Container.js

  1. import .....
  2. .... //some import statements
  3. function Container(){
  4. &lt;SideBar/&gt;
  5. &lt;Routes&gt;
  6. &lt;Route path=&quot;/list&quot; element={&lt;List/&gt;}/&gt;
  7. &lt;Route path=&quot;/detail&quot; element={&lt;Detail/&gt;}/&gt;
  8. &lt;/Routes&gt;
  9. }

Now the issue is when I navigate to http://<domain>/app it is showing a blank page with sidebar, which doesn't serve a purpose.

How this can be achieved ?

答案1

得分: 1

我相信这是一个可行的解决方案:

  • 侧边栏保留在 Layout 组件中,该组件使用了来自 react-router-dom 的 Outlet
  • 在没有使用 Layout 的情况下首先使用您的身份验证路由
  • 另外两个路由“包含”在 Layout 组件中,因此这些页面具有侧边栏

App.js

  1. const App = () => {
  2. return (
  3. <BrowserRouter>
  4. <Routes>
  5. <Route path="/" element={<Authentication />} />
  6. <Route path="/" element={<Layout />}>
  7. <Route path="/list" element={<List />} />
  8. <Route path="/detail" element={<Detail />} />
  9. </Route>
  10. </Routes>
  11. </BrowserRouter>
  12. );
  13. }
  14. export default App;
  1. export function Layout () {
  2. return (
  3. <div>
  4. <div className="container">
  5. <Sidebar />
  6. <Outlet />
  7. </div>
  8. </div>
  9. )
  10. }

注意:解决方案几乎直接取自这个其他问题

英文:

I believe this is a working solution:

  • The sidebar is kept in the Layout component which uses the Outlet from react-router-dom
  • Having your auth route first without using the Layout
  • The other two routes are "contained" within the Layout component so these pages have the Sidebar

App.js

  1. const App = () =&gt; {
  2. return (
  3. &lt;BrowserRouter&gt;
  4. &lt;Routes&gt;
  5. &lt;Route path=&quot;/&quot; element={&lt;Authentication /&gt;} /&gt;
  6. &lt;Route path=&quot;/&quot; element={&lt;Layout /&gt;}&gt;
  7. &lt;Route path=&quot;/list&quot; element={&lt;List /&gt;} /&gt;
  8. &lt;Route path=&quot;/detail&quot; element={&lt;Detail /&gt;} /&gt;
  9. &lt;/Route&gt;
  10. &lt;/Routes&gt;
  11. &lt;/BrowserRouter&gt;
  12. );
  13. }
  14. export default App;
  1. export function Layout () {
  2. return (
  3. &lt;div&gt;
  4. &lt;div className=&quot;container&quot;&gt;
  5. &lt;Sidebar /&gt;
  6. &lt;Outlet /&gt;
  7. &lt;/div&gt;
  8. &lt;/div&gt;
  9. )
  10. }

Note: Solution taken almost directly from This other question

huangapple
  • 本文由 发表于 2023年7月28日 01:18:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782103.html
匿名

发表评论

匿名网友

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

确定