上QQ阅读APP看书,第一时间看更新
How it works...
Let's start by taking a look at the state of your component, as follows:
const [open, setOpen] = useState(false);
const [content, setContent] = useState('Home');
const [items] = useState({
cpu: [
{ label: 'Add CPU', Icon: AddIcon },
{ label: 'Remove CPU', Icon: RemoveIcon },
{ label: 'Usage', Icon: ShowChartIcon }
],
memory: [
{ label: 'Add Memory', Icon: AddIcon },
{ label: 'Usage', Icon: ShowChartIcon }
],
storage: [
{ label: 'Add Storage', Icon: AddIcon },
{ label: 'Usage', Icon: ShowChartIcon }
],
network: [
{ label: 'Add Network', Icon: AddIcon, disabled: true },
{ label: 'Usage', Icon: ShowChartIcon }
]
});
Instead of the items state being a flat array of items, it's now an object with arrays grouped by category. These are the drawer sections that you want to render. Next, let's look at the List markup for rendering the items state and the section headers:
<List>
<ListSubheader>CPU</ListSubheader>
<ListItems items={items.cpu} onClick={onClick} />
<ListSubheader>Memory</ListSubheader>
<ListItems items={items.memory} onClick={onClick} />
<ListSubheader>Storage</ListSubheader>
<ListItems items={items.storage} onClick={onClick} />
<ListSubheader>Network</ListSubheader>
<ListItems items={items.network} onClick={onClick} />
</List>
The ListSubheader component is used when you need a label above the list items. For example, underneath the Storage header, you have the ListItems component that renders items from the items.storage state.