Answer the question
In order to leave comments, you need to log in
How to pass props through react router?
Good day. I'm learning React and started learning react-router v4. I have a wrapper component in the middle of which there are paths for the components
const User = props => (
<Wrapper {...props}>
<Route path='/users/:id' component={AboutComponent} exact />
<Route path='/users/:id/photos' component={PhotosComponent} />
<Route path='/users/:id/friends' component={FriendsComponent} />
</Wrapper>
);
const Loading = () => <div>Loading...</div>;
const AboutComponent = Loadable({
loader: () => import('./About'),
loading: Loading
});
const PhotosComponent = Loadable({
loader: () => import('./Photos'),
loading: Loading
});
const FriendsComponent = Loadable({
loader: () => import('./Friends'),
loading: Loading
});
render() {
const childrenWithProps = React.Children.map(this.props.children, child => {
return React.cloneElement(child, { user: 'Elon Musk' });
});
console.log(childrenWithProps);
return (
<div id="wrapper">
<div className="row">
<div className="col-lg-3 ">
<img
className="img-fluid"
src="/avatar-2.jpg"
alt="Elon Musk" />
</div>
<div className="col-lg-9 user-info">
<h2>Elon Musk</h2>
<nav>
<Link to={`/users/${this.props.user.id}`} exact>
About
</Link>
<span className="mx-1"></span>
<Link to={`/users/${this.props.user.id}/photos`}>
Photos
</Link>
<span className="mx-1"></span>
<Link to={`/affiliates/users/${this.props.user.id}/friends`}>
Friends
</Link>
</nav>
</div>
</div>
{childrenWithProps}
</div>
);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question