Reactjs 使用react-router-dom-from-map方法创建路由,但它';它没有重定向到JSX组件

Reactjs 使用react-router-dom-from-map方法创建路由,但它';它没有重定向到JSX组件,reactjs,react-router,Reactjs,React Router,我还在学习,只是停留在这个-> 所以基本上我想这样做:- <Switch> <Route path="/123" exact component={abcHandler} /> <Route path="/456" exact component={abcHandler} /> <Route path="/789" exact component={abcHa

我还在学习,只是停留在这个-> 所以基本上我想这样做:-

<Switch>
      <Route path="/123" exact component={abcHandler} />
      <Route path="/456" exact component={abcHandler} />
      <Route path="/789" exact component={abcHandler} />
</Switch>

上面的代码运行良好。 但我也有其他路径和组件,所以我尝试使用map方法,但由于某些原因,它不起作用

const abc = ['123','456','789'];
<Switch>
          {abc.map(m=>{
            <Route path={`/${m}`} exact component={abcHandler} />
          })}
          </Switch> 
const abc=['123'、'456'、'789'];
{abc.map(m=>{
})}
首先,这不起作用的原因和

是否有任何方法可以执行上述任务,以便我可以将多个路径重定向到单个组件,而无需重复相同的行?

从地图块内部返回

 const abc = ['123','456','789'];
    <Switch>
              {abc.map(m=>{
                return <Route path={`/${m}`} exact component={abcHandler} />
              })}
    </Switch> 
const abc=['123'、'456'、'789'];
{abc.map(m=>{
返回
})}

从地图块内部返回

 const abc = ['123','456','789'];
    <Switch>
              {abc.map(m=>{
                return <Route path={`/${m}`} exact component={abcHandler} />
              })}
    </Switch> 
const abc=['123'、'456'、'789'];
{abc.map(m=>{
返回
})}
您需要在
.map()
方法上使用
()
而不是
{}
,在该方法中,您可以映射数组以返回每个生成的JSX
{}
只执行其中的代码,不返回任何内容,除非使用
return something
语句指定

const abc = ['123','456','789'];
<Switch>
      {abc.map(m => (
        <Route path={`/${m}`} exact component={abcHandler} />
      ) )}
</Switch>
您需要在
.map()
方法上使用
()
而不是
{}
,在该方法中映射数组以返回每个生成的JSX
{}
只执行其中的代码,不返回任何内容,除非使用
return something
语句指定

const abc = ['123','456','789'];
<Switch>
      {abc.map(m => (
        <Route path={`/${m}`} exact component={abcHandler} />
      ) )}
</Switch>

哦。我的错。如果你认为这是你问题的解决办法,请把它标记为答案。我的错。thx如果您认为这是您问题的解决方案,请将其标记为答案。thx代表insightthx代表Insights