I have a React application that uses a Drawer to wrap child components in it.
A child component is rendered when a <Route>
matches the path in a <Switch>
.
In the stateful Drawer component, I want to handle events raised by clicks in the toolbar or sidebar, but currently, I use this.props.children.props.history.push("...")
to access the history object to navigate to another page.
What if there are two child components in that wrapper?
This is how I wrap each component with a Drawer.
const RouteWithDrawerLayout = ({ component: Component, ...rest }) => {
return (
<Route
{...rest}
render={props => (
<PersistentDrawer>
<Component {...props} />
</PersistentDrawer>
)}
/>
);
};
<Switch>
<RouteWithDrawerLayout path="/listing" exact component={Listing} />
<RouteWithDrawerLayout path="/register" exact component={Register} />
<RouteWithDrawerLayout path="/auth" exact component={Auth} />
<RouteWithDrawerLayout path="*" component={NotFoundPage} />
</Switch>
This is how I handle events in the Drawer.
handleClickLogo = () => {
this.props.children.props.history.push('/');
};