【发布时间】:2022-11-17 12:35:10
【问题描述】:
我对 React 比较陌生,我想知道这里的标准是什么。
想象一下我有一个像这样的反应路由器:
<Router history={history}>
<Route path="/" component={App}>
<Route path="home component={Home} />
<Route path="about" component={About} />
<Route path="inbox" component={Inbox} />
<Route path="contacts" component={Contacts} />
</Route>
</Router>
现在,如果prop.mail 设置为false,我想删除两条路线,所以理智的做法如下所示:
<Router history={history}>
<Route path="/" component={App}>
<Route path="home component={Home} />
<Route path="about" component={About} />
{ if.this.props.mail ?
<Route path="inbox" component={Inbox} />
<Route path="contacts" component={Contacts} />
: null }
</Route>
</Router>
但是有 2 条路线,React 返回错误:
表达式必须有一个父元素。
我不想在这里使用多个 if。首选的 React 处理方式是什么?
【问题讨论】:
标签: reactjs react-router