[英]How to perform dynamic component rendering using react router v4?
I have an application with a Login page followed by a Dashboard page. 我有一个带有“登录”页面和“仪表板”页面的应用程序。 The routes that I've defined in the
index.js
are like this: 我在
index.js
中定义的路由是这样的:
<Router>
<div>
<Route exact path="/" component={Login}/>
<Route path="/dashboard" component={Dashboard} />
</div>
</Router>
Dashboard.js: Dashboard.js:
return (
<div>
<Header/>
<Footer/>
<Switch>
<Route path="/dashboard/content1" component={content1} />
<Route path="/dashboard/content2" component={content2} />
</Switch>
</div>
);
The Dashboard
component is rendering 3 of its child components. Dashboard
组件正在渲染其子组件中的3个。 Header
, Footer
and Content1
. Header
, Footer
和Content1
。 I want the Dashboard component to render Content1
by default (ie when the url is /dashboard
) and also when the url is /dashboard/content1
, and should render content2
when the url is /dashboard/content2
. 我想在仪表板组件呈现
Content1
默认情况下(即当网址/dashboard
),也当网址/dashboard/content1
,并且应该呈现content2
当网址/dashboard/content2
。 Header
& Footer
components should remain. Header
和Footer
组件应保留。 Please suggest the configuration for the Dashboard
component to achieve the same. 请建议
Dashboard
组件的配置以实现相同的目的。
In React-router v4 you provide Routes within the component, so you can write your Routes as follows 在React-router v4中,您在组件内提供了Routes,因此您可以按以下方式编写Routes
<Router>
<div>
<Route exact path="/" component={Login}/>
<Route path="/dashboard" component={Dashboard} />
</div>
</Router>
and then in the Dashboard Component render method 然后在“仪表板组件”渲染方法中
render() {
return (
<div>
{/* other content */}
<Switch>
<Route exact path="/dashboard" component={content1} />
<Route path="/dashboard/content1" component={content1} />
<Route path="/dashboard/content2" component={content2} />
</Switch>
</div>
)
}
As a variant of an answer to your post before you have edited it, you can do it (nesting) like this: 在编辑帖子之前,可以将其作为答案的一种变体,您可以按照以下方式进行(嵌套):
<Router>
<Header/>
<Content1/>
<Footer/>
<Switch>
<Route exact path="/" component={Login}/>
<Route exact path="/dashboard" component={Dashboard} />
<Route exact path="/dashboard/content1" component={content1} />
<Route exact path="/dashboard/content2" component={content2} />
</Switch>
</Router>
React-Router's Switch
component renders the first thing that matches, so if you put a route without a path last, it will render that if no other routes match, essentially treating it as a default. React-Router的
Switch
组件呈现匹配的第一件事,因此,如果您放置的路由最后没有路径,它将呈现出如果没有其他路由匹配的情况,则将其视为默认值。 Like so: 像这样:
return (
<div>
<Header/>
<Footer/>
<Switch>
<Route path="/dashboard/content2" exact component={Content2} />
<Route component={Content1} />
</Switch>
</div>
);
I have found that rendering a component instead of a Route also works, although I dont know if it's officially supported. 我发现渲染组件而不是Route也可以,尽管我不知道它是否得到正式支持。
return (
<div>
<Header/>
<Footer/>
<Switch>
<Route path="/dashboard/content2" exact component={Content2} />
<Content1 />
</Switch>
</div>
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.