【发布时间】:2021-11-09 06:17:09
【问题描述】:
我对 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