Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
React Material-UI Cookbook

You're reading from   React Material-UI Cookbook Build captivating user experiences using React and Material-UI

Arrow left icon
Product type Paperback
Published in Mar 2019
Publisher Packt
ISBN-13 9781789615227
Length 534 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Adam Boduch Adam Boduch
Author Profile Icon Adam Boduch
Adam Boduch
Arrow right icon
View More author details
Toc

Table of Contents (22) Chapters Close

Preface 1. Grids - Placing Components on the Page FREE CHAPTER 2. App Bars - The Top Level of Every Page 3. Drawers - A Place for Navigation Controls 4. Tabs - Grouping Content into Tab Sections 5. Expansion Panels - Group Content into Panel Sections 6. Lists - Display Simple Collection Data 7. Tables - Display Complex Collection Data 8. Cards - Display Detailed Information 9. Snackbars - Temporary Messages 10. Buttons - Initiating Actions 11. Text - Collecting Text Input 12. Autocomplete and Chips - Text Input Suggestions for Multiple Items 13. Selection - Make Selections from Choices 14. Pickers - Selecting Dates and Times 15. Dialogs - Modal Screens for User Interactions 16. Menus - Display Actions That Pop Out 17. Typography - Control Font Look and Feel 18. Icons - Enhance Icons to Match Your Look and Feel 19. Themes - Centralize the Look and Feel of Your App 20. Styles - Applying Styles to Components 21. Other Books You May Enjoy

Abstracting containers and items

You have lots of screens in your app, each with lots of Grid components, used to create complex layouts. Trying to read source code that has a ton of <Grid> elements in it can be daunting. Especially when a Grid component is used for both containers and for items.

How to do it...

The container or the item property of Grid components determines the role of the element. You can create two components that use these properties and create an element name that's easier to read when you have lots of layout components:

import React from 'react';

import { withStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
import Grid from '@material-ui/core/Grid';

const styles = theme => ({
root: {
flexGrow: 1
},
paper: {
padding: theme.spacing(2),
textAlign: 'center',
color: theme.palette.text.secondary
}
});

const Container = props => <Grid container {...props} />;
const Item = props => <Grid item {...props} />;

const AbstractingContainersAndItems = withStyles(styles)(
({ classes }) => (
<div className={classes.root}>
<Container spacing={4}>
<Item xs={12} sm={6} md={3}>
<Paper className={classes.paper}>xs=12 sm=6 md=3</Paper>
</Item>
<Item xs={12} sm={6} md={3}>
<Paper className={classes.paper}>xs=12 sm=6 md=3</Paper>
</Item>
<Item xs={12} sm={6} md={3}>
<Paper className={classes.paper}>xs=12 sm=6 md=3</Paper>
</Item>
<Item xs={12} sm={6} md={3}>
<Paper className={classes.paper}>xs=12 sm=6 md=3</Paper>
</Item>
</Container>
</div>
)
);

export default AbstractingContainersAndItems;

Here's what the resulting layout looks like:

How it works...

Let's take a closer look at the Container and Item components:

const Container = props => <Grid container {...props} />;
const Item = props => <Grid item {...props} />;

The Container component renders a Grid component with the container property set to true, and the Item component does the same, except with the item property set to true. Each component passes any additional properties to the Grid component, such as xs and sm breakpoints.

When you have lots of Grid containers and items that make up your layout, being able to see the difference between <Container> and <Item> elements makes your code that much easier to read. Contrast this with having <Grid> elements everywhere.

There's more...

If you find that you're using the same breakpoints over and over in your layouts, you can include them in in your higher-order Item component. Let's rewrite the example so that, in addition to the Item property, the xs, sm, and md properties are included as well:

const Container = props => <Grid container {...props} />;
const Item = props => <Grid item xs={12} sm={6} md={3} {...props} />;

const AbstractingContainersAndItems = withStyles(styles)(
({ classes }) => (
<div className={classes.root}>
<Container spacing={4}>
<Item>
<Paper className={classes.paper}>xs=12 sm=6 md=3</Paper>
</Item>
<Item>
<Paper className={classes.paper}>xs=12 sm=6 md=3</Paper>
</Item>
<Item>
<Paper className={classes.paper}>xs=12 sm=6 md=3</Paper>
</Item>
<Item>
<Paper className={classes.paper}>xs=12 sm=6 md=3</Paper>
</Item>
</Container>
</div>
)
);

Now, instead of four instances of <Item xs={12} sm={6} md={3}>, you have four instances of <Item>. Component abstractions are a great tool for removing excess syntax from your JavaScript XML (JSX) markup.

Any time you need to override any of the breakpoint properties that you've set in the Item component, you just need to pass the property to Item. For example, if you have a specific case where you need md to be 6, you can just write <Item md={6}>. This works because, in the Item component, {...props} is passed after the default values, meaning that they override any properties with the same name.

See also

You have been reading a chapter from
React Material-UI Cookbook
Published in: Mar 2019
Publisher: Packt
ISBN-13: 9781789615227
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime