高阶组件和 Material-UI 的 makeStyles 的样式问题

Posted

技术标签:

【中文标题】高阶组件和 Material-UI 的 makeStyles 的样式问题【英文标题】:Style problems with higher order component & Material-UI's makeStyles 【发布时间】:2020-06-27 17:25:42 【问题描述】:

我正在努力避免每个How to style components using makeStyles and still have lifecycle methods in Material UI? 的无效挂钩调用错误。

无法说出我在这里可能做错了什么。类名似乎生成得很好,但样式却不是。请参阅类代码下方的 .RecipeReviewCard-avatar-10 的定义检查器返回。找到完整的可运行示例 here on codesandbox。似乎 makeStyles 没有执行,但是如果我执行 withStyles(styles())(RecipeReviewCard) 我会得到相同的钩子错误。

const styles = makeStyles(theme => (
  root: 
    maxWidth: 345
  ,
  ...snipped for brevity...
  avatar: 
    backgroundColor: red[500]
  
));

class RecipeReviewCard extends React.Component 
  constructor(props) 
    super(props);
    this.state = 
      expanded: false,
      anchorEl: null,
      open: false
    ;

    this.handleExpandClick = this.handleExpandClick.bind(this);
    this.handleClickAway = this.handleClickAway.bind(this);
    this.onClick = this.onClick.bind(this);
  

  render() 
    const classes = this.props.classes;
    ... snipped for brevity...


export default withStyles(styles)(RecipeReviewCard);

.RecipeReviewCard-avatar-10 
    0: m;
    1: a;
    2: k;
    3: e;
    4: S;
    5: t;
    6: y;
    7: l;
    8: e;
    9: s;
    10: -;
    11: a;
    12: v;
    13: a;
    14: t;
    15: a;
    16: r;
    17: -;
    18: 5;

帮助赞赏

【问题讨论】:

【参考方案1】:

您的沙盒中有几个主要问题:

您不应该将makeStyleswithStyles 混用。 makeStyles 只能用于创建要在函数组件中调用的自定义挂钩。只需删除 makeStyles 调用即可。 您应该从 @material-ui/core/styles 而不是从 @material-ui/styles 导入 withStyles,否则它将无法访问默认主题。

这是您的沙盒的工作(至少是样式)版本:

import React from "react";
import  withStyles  from "@material-ui/core/styles";
import clsx from "clsx";
import Card from "@material-ui/core/Card";
import CardHeader from "@material-ui/core/CardHeader";
import CardContent from "@material-ui/core/CardContent";
import CardActions from "@material-ui/core/CardActions";
import Collapse from "@material-ui/core/Collapse";
import Avatar from "@material-ui/core/Avatar";
import IconButton from "@material-ui/core/IconButton";
import Typography from "@material-ui/core/Typography";
import  red  from "@material-ui/core/colors";
import FavoriteIcon from "@material-ui/icons/Favorite";
import ShareIcon from "@material-ui/icons/Share";
import ExpandMoreIcon from "@material-ui/icons/ExpandMore";
import MoreVertIcon from "@material-ui/icons/MoreVert";
import Popper from "@material-ui/core/Popper";
import Paper from "@material-ui/core/Paper";
import ClickAwayListener from "@material-ui/core/ClickAwayListener";

import  Input, Checkbox  from "@progress/kendo-react-inputs";

const styles = theme => (
  root: 
    maxWidth: 345
  ,
  media: 
    height: 0,
    paddingTop: "56.25%" // 16:9
  ,
  expand: 
    transform: "rotate(0deg)",
    marginLeft: "auto",
    transition: theme.transitions.create("transform", 
      duration: theme.transitions.duration.shortest
    )
  ,
  expandOpen: 
    transform: "rotate(180deg)"
  ,
  avatar: 
    backgroundColor: red[500]
  
);

class RecipeReviewCard extends React.Component 
  constructor(props) 
    super(props);
    this.state = 
      expanded: false,
      anchorEl: null,
      open: false
    ;

    this.handleExpandClick = this.handleExpandClick.bind(this);
    this.handleClickAway = this.handleClickAway.bind(this);
    this.onClick = this.onClick.bind(this);
  

  render() 
    const classes = this.props.classes;
    console.log(classes);
    const isNew = this.props.isNew || false;
    const iconClass = clsx(classes.expand, 
      [classes.expandOpen]: this.state.expanded
    );
    return (
      <div className="ccard">
        <Popper
          open=this.state.open
          anchorEl=this.state.anchorEl
          placement="bottom-end"
          transition
        >
          ( TransitionProps ) => (
            <Paper>
              <Typography className=classes.typography>
                Delete?
                <br />
                Other?
              </Typography>
            </Paper>
          )
        </Popper>
        <Card className=classes.root>
          <CardHeader
            avatar=
              <Avatar aria-label="recipe" className=classes.avatar>
                R
              </Avatar>
            
            action=
              <ClickAwayListener onClickAway=this.handleClickAway>
                <IconButton aria-label="settings" onClick=this.onClick>
                  <MoreVertIcon />
                </IconButton>
              </ClickAwayListener>
            
            title="Shrimp and Chorizo Paella"
            subheader="September 14, 2016"
          />
          <CardContent>
            <div className="">
              <Input
                name="ID"
                style= width: "100%" 
                label="ID*"
                pattern="[A-Za-z]+"
                minLength=2
                required=true
                readOnly=!isNew
                disabled=!isNew
                onChange=this.onChange
                value=this.state.id
              />
            </div>
            <div className="mb-3">
              <Input
                name="Description"
                style= width: "100%" 
                label="Description"
                pattern="[A-Za-z]+"
                minLength=2
                onChange=e => this.onChange(e, "description")
                value=this.state.description
              />
            </div>
            <div className="mb-3">
              <Checkbox
                id="chb1"
                label="Active"
                name="active"
                labelPlacement="before"
                onChange=e => this.onChange(e, "active")
                value=this.state.active
              />
            </div>
          </CardContent>
          <CardActions disableSpacing>
            <IconButton aria-label="add to favorites">
              <FavoriteIcon />
            </IconButton>
            <IconButton aria-label="share">
              <ShareIcon />
            </IconButton>
            <IconButton
              className=iconClass
              onClick=this.handleExpandClick
              aria-expanded=this.state.expanded
              aria-label="show more"
            >
              <ExpandMoreIcon />
            </IconButton>
          </CardActions>
          <Collapse in=this.state.expanded timeout="auto" unmountOnExit>
            <CardContent>
              <Typography paragraph>Method:</Typography>
              <Typography paragraph>
                Heat 1/2 cup of the broth in a pot until simmering, add saffron
                and set aside for 10 minutes.
              </Typography>
              <Typography paragraph>
                Heat oil in a (14- to 16-inch) paella pan or a large, deep
                skillet over medium-high heat. Add chicken, shrimp and chorizo,
                and cook, stirring occasionally until lightly browned, 6 to 8
                minutes. Transfer shrimp to a large plate and set aside, leaving
                chicken and chorizo in the pan. Add pimentón, bay leaves,
                garlic, tomatoes, onion, salt and pepper, and cook, stirring
                often until thickened and fragrant, about 10 minutes. Add
                saffron broth and remaining 4 1/2 cups chicken broth; bring to a
                boil.
              </Typography>
              <Typography paragraph>
                Add rice and stir very gently to distribute. Top with artichokes
                and peppers, and cook without stirring, until most of the liquid
                is absorbed, 15 to 18 minutes. Reduce heat to medium-low, add
                reserved shrimp and mussels, tucking them down into the rice,
                and cook again without stirring, until mussels have opened and
                rice is just tender, 5 to 7 minutes more. (Discard any mussels
                that don’t open.)
              </Typography>
              <Typography>
                Set aside off of the heat to let rest for 10 minutes, and then
                serve.
              </Typography>
            </CardContent>
          </Collapse>
        </Card>
      </div>
    );
  

  handleExpandClick() 
    this.setState( expanded: !this.state.expanded );
  

  onClick(event) 
    this.setState(
      anchorEl: event.currentTarget,
      open: !this.state.open
    );
  

  handleClickAway() 
    this.setState(
      open: false
    );
  

  onChange() 


export default withStyles(styles)(RecipeReviewCard);

相关答案:

How to use theme in styles for custom class components What is the benefit of using withStyles over makeStyles? Material-UI withStyles doesn't apply any kind of styles Cannot import @material-ui/core/styles/MuiThemeProvider

【讨论】:

以上是关于高阶组件和 Material-UI 的 makeStyles 的样式问题的主要内容,如果未能解决你的问题,请参考以下文章

如何在 material-ui 样式/JSS 中选择 id?

material-ui makeStyles:如何按标签名称设置样式?

使用带有样式组件的 Material-UI 主题

Material-ui 与其他非 mui 反应组件的兼容性

在 Material-ui Autocomplete 组件上设置文本颜色、轮廓和填充

如何在 Material-UI 中使用 Box 组件覆盖按钮?