【发布时间】:2019-10-16 13:55:09
【问题描述】:
我试图从 Material UI 文档 (https://material-ui.com/components/grid/) 中的示例中理解此代码示例 https://codesandbox.io/s/9rvlm:
import React from 'react';
import PropTypes from 'prop-types';
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,
},
});
function CenteredGrid(props) {
const { classes } = props;
return (
<div className={classes.root}>
<Grid container spacing={3}>
<Grid item xs={12}>
<Paper className={classes.paper}>xs=12</Paper>
</Grid>
<Grid item xs={6}>
<Paper className={classes.paper}>xs=6</Paper>
</Grid>
<Grid item xs={6}>
<Paper className={classes.paper}>xs=6</Paper>
</Grid>
<Grid item xs={3}>
<Paper className={classes.paper}>xs=3</Paper>
</Grid>
<Grid item xs={3}>
<Paper className={classes.paper}>xs=3</Paper>
</Grid>
<Grid item xs={3}>
<Paper className={classes.paper}>xs=3</Paper>
</Grid>
<Grid item xs={3}>
<Paper className={classes.paper}>xs=3</Paper>
</Grid>
</Grid>
</div>
);
}
CenteredGrid.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(CenteredGrid);
我的问题是:将flexGrow: 1 分配给父div 元素的目的是什么?
据我了解https://material-ui.com/system/flexbox/#flex-grow 和https://developer.mozilla.org/en-US/docs/Web/CSS/flex-grow,flex-grow 是弹性容器项目的 CSS 属性。但是在这个例子中,我没有看到包含这个组件的弹性容器元素; CenteredGrid 直接显示在根 div 中(如 <Demo/>)。
styles.root 是否应用于父 div 以防万一组件在 flex 容器中呈现?非常感谢您的澄清。
【问题讨论】:
-
如果我错了,请纠正我,但我认为它的存在没有意义。唯一会产生影响的情况是当有一个 flex 父级时。
标签: css reactjs flexbox material-ui