如何在同一页上使用多个路径?

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

How to use multiple paths on the same page?

问题

我有一个组件,我想要创建一个对象或编辑一个对象,基本上我有相同的路径,只是取决于是否有一个id,但是最近react-router-dom不允许我将字符串数组作为参数传递。我该如何实现相同的结果?

  return (
    <>
      <NavBar />
      <Container style={{marginTop: '7em'}}>
        <Routes>
          <Route path='/' Component={HomePage}/>                
          <Route path='/arenas' Component={ArenaDashboard}/>    
          <Route path='/arenas/:id' Component={ArenaDetails}/>    
          <Route path={['/createArena', '/manage/:id']} Component={ArenaForm} />    
        </Routes>
      </Container>
    </>
  );
英文:

I have a component in which I want to create an object or to edit an object, basically I have the same path, it only depends if it got an id or not, but lately the react-router-dom doesn't allow me to give an array of strings as a parameter. How can I achieve the same result?

  return (
    &lt;&gt;
      &lt;NavBar /&gt;
      &lt;Container style={{marginTop: &#39;7em&#39;}}&gt;
        &lt;Routes&gt;
          &lt;Route path=&#39;/&#39; Component={HomePage}/&gt;                
          &lt;Route path=&#39;/arenas&#39; Component={ArenaDashboard}/&gt;    
          &lt;Route path=&#39;/arenas/:id&#39; Component={ArenaDetails}/&gt;    
          &lt;Route path={[&#39;/createArena&#39;, &#39;/manage/:id&#39;]} Component={ArenaForm} /&gt;    
        &lt;/Routes&gt;
      &lt;/Container&gt;
    &lt;/&gt;
  );

答案1

得分: 1

我找到了如何做到这一点:

const location = useLocation();

  return (
    <>
      <NavBar />
      <Container style={{marginTop: '7em'}}>
        <Routes>
          <Route path='/' Component={HomePage}/>               
          <Route path='/arenas' Component={ArenaDashboard}/>    
          <Route path='/arenas/:id' Component={ArenaDetails}/>    
          {['/createArena', '/manage/:id'].map((path) => (
            <Route key={location.key} path={path} element={<ArenaForm />} />
          ))} 
        </Routes>
      </Container>
    </>
  );
}

请注意,这是一段JavaScript代码,用于在React应用程序中定义路由。

英文:

I found how to do it:

const location = useLocation();

  return (
    &lt;&gt;
      &lt;NavBar /&gt;
      &lt;Container style={{marginTop: &#39;7em&#39;}}&gt;
        &lt;Routes&gt;
          &lt;Route path=&#39;/&#39; Component={HomePage}/&gt;               
          &lt;Route path=&#39;/arenas&#39; Component={ArenaDashboard}/&gt;    
          &lt;Route path=&#39;/arenas/:id&#39; Component={ArenaDetails}/&gt;    
          {[&#39;/createArena&#39;, &#39;/manage/:id&#39;].map((path) =&gt; (
            &lt;Route key={location.key} path={path} element={&lt;ArenaForm /&gt;} /&gt;
          ))} 
        &lt;/Routes&gt;
      &lt;/Container&gt;
    &lt;/&gt;
  );
}```



</details>



# 答案2
**得分**: 0

你可以明确将其定义为:
```jsx
return (
  <>
    <NavBar />
    <Container style={{marginTop: '7em'}}>
      <Routes>
        <Route path='/' Component={HomePage}/>                
        <Route path='/arenas' Component={ArenaDashboard}/>    
        <Route path='/arenas/:id' Component={ArenaDetails}/>    
        <Route path='/createArena' Component={ArenaForm} />
        <Route path='/manage/:id' Component={ArenaForm} />     
      </Routes>
    </Container>
  </>
);
英文:

You may explicitly define it as:

return (
    &lt;&gt;
      &lt;NavBar /&gt;
      &lt;Container style={{marginTop: &#39;7em&#39;}}&gt;
        &lt;Routes&gt;
          &lt;Route path=&#39;/&#39; Component={HomePage}/&gt;                
          &lt;Route path=&#39;/arenas&#39; Component={ArenaDashboard}/&gt;    
          &lt;Route path=&#39;/arenas/:id&#39; Component={ArenaDetails}/&gt;    
          &lt;Route path=&#39;/createArena&#39; Component={ArenaForm} /&gt;
          &lt;Route path=&#39;/manage/:id&#39; Component={ArenaForm} /&gt;     
        &lt;/Routes&gt;
      &lt;/Container&gt;
    &lt;/&gt;
  );

huangapple
  • 本文由 发表于 2023年7月27日 15:38:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777493.html
匿名

发表评论

匿名网友

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

确定