I have a Component named Layout
that should render different child components
depending on the route.
Parent component
export default class Layout extends Component{
render(){
return (
<div>
<div>
<div>
<Navbar/>
{this.props.sidebar}
{this.props.content}
</div>
</div>
</div>
);
}
}
In my main.jxs
i want to render 3 Route using the Layout component
with different ChildComponent
passed as sidebar and content props like:
<Route path="/profile" component={sidebar: <Sidebar />, content: <Content />} />
<Route path="/user" component={sidebar: <Sidebar />, content: <User />} />
<Route path="/edit" component={sidebar: <Sidebar />, content: <Edit />} />
Components are imported as well. In other words, i want to dynamically change the content of layout based on the route. How can i achieve this using react-router-dom
?
If I got you right you should use render func as prop in Route component (react router 4) In order to can render a several components. See
https://reacttraining.com/react-router/web/api/Route/render-func
Also I provide next small example
https://jsfiddle.net/69z2wepo/85086/