如何在 material ui 中保持一致的宽度
How to have consistent width in material ui
我有两个目标组件,我不知道是什么导致它们的宽度以这种方式表现
我尝试将纸张选择器的宽度设置为 100%,但它对每个目标组件都没有影响。 我如何响应性地在此目标组件的每一侧设置一致的宽度?可能是这样的......黑框代表网页,红框代表目标组件
当前代码如下,供参考:
import React, { useEffect } from "react";
import Moment from "react-moment";
import PropTypes from "prop-types";
import { Link } from "react-router-dom";
import { connect } from "react-redux";
import { getGoals } from "../../actions/goal";
import Spinner from "../layout/Spinner";
import Navbar from "../dashboard/Navbar";
import ThumbUpAltIcon from "@material-ui/icons/ThumbUpAlt";
import ThumbDownAltIcon from "@material-ui/icons/ThumbDownAlt";
import ChatIcon from "@material-ui/icons/Chat";
import DeleteIcon from "@material-ui/icons/Delete";
import DoneIcon from "@material-ui/icons/Done";
import {
Typography,
Container,
CssBaseline,
makeStyles,
Grid,
Avatar,
Paper
} from "@material-ui/core";
const useStyles = makeStyles(theme => ({
paper: {
height: "auto"
},
actionButtons: {
marginTop: "3vh"
},
profileHeader: {
textAlign: "center",
marginBottom: 20
},
avatar: {
width: theme.spacing(7),
height: theme.spacing(7)
}
}));
const Goals = ({ getGoals, auth, goal: { goals, user, loading } }) => {
useEffect(() => {
getGoals();
}, [getGoals]);
const classes = useStyles();
return loading ? (
<>
<Navbar />
<Container component="main" maxWidth="xs">
<CssBaseline />
<div className={classes.paper}>
<Spinner />
</div>
</Container>
</>
) : (
<>
<CssBaseline />
<Navbar />
<main>
<Container>
<Typography variant="h2" className={classes.profileHeader}>
Goals
</Typography>
{/* parent grid */}
<Grid container spacing={4}>
{goals.map(singleGoal => (
<Paper key={singleGoal._id}>
<Grid
className={classes.paper}
spacing={1}
container
direction="row"
alignItems="center"
>
<Grid
item
container
direction="column"
justify="center"
alignItems="center"
xs={3}
>
<Avatar
className={classes.avatar}
src={singleGoal.avatar}
/>
<Typography variant="caption">{singleGoal.first_name} {singleGoal.last_name}</Typography>
<Typography variant="caption" className={classes.postedOn}>
Posted on{" "}
<Moment format="MM/DD/YYYY">{singleGoal.date}</Moment>
</Typography>
</Grid>
<Grid container item direction="column" xs={9}>
<Typography variant="body1">
{singleGoal.text}
</Typography>
<Grid item className={classes.actionButtons}>
<ThumbUpAltIcon />
<ThumbDownAltIcon />
<ChatIcon />
<DoneIcon />
<DeleteIcon />
</Grid>
</Grid>
</Grid>
</Paper>
))}
</Grid>
</Container>
</main>
</>
);
};
Goals.propTypes = {
getGoals: PropTypes.func.isRequired,
goal: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
goal: state.goal,
auth: state.auth
});
export default connect(mapStateToProps, { getGoals })(Goals);
奇怪的行为是因为您将组件包装在 <Paper>
中。 Grid 容器应填充 Grid 项以使其按预期运行。所以删除 <Paper key={singleGoal._id}>
应该会改善很多。然后,您可以使用 CSS 将内部 Grid 项设为白色,甚至可以将 component={Paper}
添加到它的属性中。
所以像这样
<Grid container spacing={4}>
{goals.map(singleGoal => (
<Grid
className={classes.paper}
spacing={1}
container
direction="row"
alignItems="center"
component={Paper}
>
<Grid
item
container
direction="column"
justify="center"
alignItems="center"
xs={3}
>
<Avatar className={classes.avatar} src={singleGoal.avatar} />
<Typography variant="caption">
{singleGoal.first_name} {singleGoal.last_name}
</Typography>
<Typography variant="caption" className={classes.postedOn}>
Posted on <Moment format="MM/DD/YYYY">{singleGoal.date}</Moment>
</Typography>
</Grid>
<Grid container item direction="column" xs={9}>
<Typography variant="body1">{singleGoal.text}</Typography>
<Grid item className={classes.actionButtons}>
<ThumbUpAltIcon />
<ThumbDownAltIcon />
<ChatIcon />
<DoneIcon />
<DeleteIcon />
</Grid>
</Grid>
</Grid>
))}
</Grid>;
我有两个目标组件,我不知道是什么导致它们的宽度以这种方式表现
我尝试将纸张选择器的宽度设置为 100%,但它对每个目标组件都没有影响。 我如何响应性地在此目标组件的每一侧设置一致的宽度?可能是这样的......黑框代表网页,红框代表目标组件
当前代码如下,供参考:
import React, { useEffect } from "react";
import Moment from "react-moment";
import PropTypes from "prop-types";
import { Link } from "react-router-dom";
import { connect } from "react-redux";
import { getGoals } from "../../actions/goal";
import Spinner from "../layout/Spinner";
import Navbar from "../dashboard/Navbar";
import ThumbUpAltIcon from "@material-ui/icons/ThumbUpAlt";
import ThumbDownAltIcon from "@material-ui/icons/ThumbDownAlt";
import ChatIcon from "@material-ui/icons/Chat";
import DeleteIcon from "@material-ui/icons/Delete";
import DoneIcon from "@material-ui/icons/Done";
import {
Typography,
Container,
CssBaseline,
makeStyles,
Grid,
Avatar,
Paper
} from "@material-ui/core";
const useStyles = makeStyles(theme => ({
paper: {
height: "auto"
},
actionButtons: {
marginTop: "3vh"
},
profileHeader: {
textAlign: "center",
marginBottom: 20
},
avatar: {
width: theme.spacing(7),
height: theme.spacing(7)
}
}));
const Goals = ({ getGoals, auth, goal: { goals, user, loading } }) => {
useEffect(() => {
getGoals();
}, [getGoals]);
const classes = useStyles();
return loading ? (
<>
<Navbar />
<Container component="main" maxWidth="xs">
<CssBaseline />
<div className={classes.paper}>
<Spinner />
</div>
</Container>
</>
) : (
<>
<CssBaseline />
<Navbar />
<main>
<Container>
<Typography variant="h2" className={classes.profileHeader}>
Goals
</Typography>
{/* parent grid */}
<Grid container spacing={4}>
{goals.map(singleGoal => (
<Paper key={singleGoal._id}>
<Grid
className={classes.paper}
spacing={1}
container
direction="row"
alignItems="center"
>
<Grid
item
container
direction="column"
justify="center"
alignItems="center"
xs={3}
>
<Avatar
className={classes.avatar}
src={singleGoal.avatar}
/>
<Typography variant="caption">{singleGoal.first_name} {singleGoal.last_name}</Typography>
<Typography variant="caption" className={classes.postedOn}>
Posted on{" "}
<Moment format="MM/DD/YYYY">{singleGoal.date}</Moment>
</Typography>
</Grid>
<Grid container item direction="column" xs={9}>
<Typography variant="body1">
{singleGoal.text}
</Typography>
<Grid item className={classes.actionButtons}>
<ThumbUpAltIcon />
<ThumbDownAltIcon />
<ChatIcon />
<DoneIcon />
<DeleteIcon />
</Grid>
</Grid>
</Grid>
</Paper>
))}
</Grid>
</Container>
</main>
</>
);
};
Goals.propTypes = {
getGoals: PropTypes.func.isRequired,
goal: PropTypes.object.isRequired
};
const mapStateToProps = state => ({
goal: state.goal,
auth: state.auth
});
export default connect(mapStateToProps, { getGoals })(Goals);
奇怪的行为是因为您将组件包装在 <Paper>
中。 Grid 容器应填充 Grid 项以使其按预期运行。所以删除 <Paper key={singleGoal._id}>
应该会改善很多。然后,您可以使用 CSS 将内部 Grid 项设为白色,甚至可以将 component={Paper}
添加到它的属性中。
所以像这样
<Grid container spacing={4}>
{goals.map(singleGoal => (
<Grid
className={classes.paper}
spacing={1}
container
direction="row"
alignItems="center"
component={Paper}
>
<Grid
item
container
direction="column"
justify="center"
alignItems="center"
xs={3}
>
<Avatar className={classes.avatar} src={singleGoal.avatar} />
<Typography variant="caption">
{singleGoal.first_name} {singleGoal.last_name}
</Typography>
<Typography variant="caption" className={classes.postedOn}>
Posted on <Moment format="MM/DD/YYYY">{singleGoal.date}</Moment>
</Typography>
</Grid>
<Grid container item direction="column" xs={9}>
<Typography variant="body1">{singleGoal.text}</Typography>
<Grid item className={classes.actionButtons}>
<ThumbUpAltIcon />
<ThumbDownAltIcon />
<ChatIcon />
<DoneIcon />
<DeleteIcon />
</Grid>
</Grid>
</Grid>
))}
</Grid>;