React Material:UI Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

Let's say that your app is composed of three pages. To navigate from page to page, you want to provide your users with links in the Drawer component. Here's what the code looks like:

import React, { useState } from 'react';
import { Route, Link } from 'react-router-dom';

import { withStyles } from '@material-ui/core/styles';
import Drawer from '@material-ui/core/Drawer';
import Grid from '@material-ui/core/Grid';
import Button from '@material-ui/core/Button';
import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import Typography from '@material-ui/core/Typography';

import HomeIcon from '@material-ui/icons/Home';
import WebIcon from '@material-ui/icons/Web';

const styles = theme => ({
alignContent: {
alignSelf: 'center'
}
});

function DrawerItemNavigation({ classes }) {
const [open, setOpen] = useState(false);

return (
<Grid container justify="space-between">
<Grid item className={classes.alignContent}>
<Route
exact
path="/"
render={() => <Typography>Home</Typography>}
/>
<Route
exact
path="/page2"
render={() => <Typography>Page 2</Typography>}
/>
<Route
exact
path="/page3"
render={() => <Typography>Page 3</Typography>}
/>
</Grid>
<Grid item>
<Drawer
className={classes.drawerWidth}
open={open}
onClose={() => setOpen(false)}
>
<List>
<ListItem
component={Link}
to="/"
onClick={() => setOpen(false)}
>
<ListItemIcon>
<HomeIcon />
</ListItemIcon>
<ListItemText>Home</ListItemText>
</ListItem>
<ListItem
component={Link}
to="/page2"
onClick={() => setOpen(false)}
>
<ListItemIcon>
<WebIcon />
</ListItemIcon>
<ListItemText>Page 2</ListItemText>
</ListItem>
<ListItem
component={Link}
to="/page3"
onClick={() => setOpen(false)}
>
<ListItemIcon>
<WebIcon />
</ListItemIcon>
<ListItemText>Page 3</ListItemText>
</ListItem>
</List>
</Drawer>
</Grid>
<Grid item>
<Button onClick={() => setOpen(!open)}>
{open ? 'Hide' : 'Show'} Drawer
</Button>
</Grid>
</Grid>
);
}

export default withStyles(styles)(DrawerItemNavigation);

When you first load the screen, you'll see the SHOW DRAWER button and the home screen content:

Here's what the drawer looks like when it's opened:

If you click on Page 2, which points to /page2, the drawer should close and you should be taken to the second page. Here's what it looks like:

You should see something similar if you click on Page 3 or on Home. The content on the left side of the screen is updated.