【发布时间】:2016-08-06 16:09:24
【问题描述】:
我想将 React Router 的路由声明移动到单独的文件中,然后自动完成路由规范,如下所示:
// Central array for routes
const routes = [];
// Two routes in separate files
routes.push(<Route path="page1" component="Component1"/>);
routes.push(<Route path="page2" component="Component2"/>);
// Render the routes
<Router history={browserHistory}>
{routes}
</Router>
虽然这可行,但我收到了警告,即迭代器的所有子节点,即路由,都应该有一个 key prop:
Each child in an array or iterator should have a unique "key" prop. 但是在这种情况下真的需要密钥吗?据我了解,即使我使用的是数组,路由也只渲染一次并且不是动态的。
是否可以在 React 中做到这一点?
【问题讨论】:
标签: javascript reactjs react-router