简体   繁体   English

自定义组件不接受样式react material ui

[英]Custom component does not accept styles react material ui

I have an Avatar Component that I have created, its a very basic component that displays an icon and some text.我有一个我创建的Avatar Component ,它是一个显示图标和一些文本的非常基本的组件。 This is the code for the component.这是组件的代码。

import React from "react";
import PropTypes from "prop-types";
import { withStyles, Avatar, Typography } from "@material-ui/core";
import avatar_placeholder from "../images/avatar_man.svg";

const styles = theme => ({
  row: {
    display: "flex",
    flexDirection: "row",
    alignItems: "center"
  },
  avatar: {
    width: 50,
    height: 50,
    marginRight: "2%"
  },
  subtitle: {
    opacity: 0.5
  }
});

const customAvatar = props => {
  const { classes, name, subtitle } = props;

  return (
    <div className={classes.row}>
      <Avatar alt={name} src={avatar_placeholder} />
      <div>
        <Typography variant="title">{name}</Typography>
        <Typography variant="body2" className={classes.subtitle}>
          {subtitle}
        </Typography>
      </div>
    </div>
  );
};

customAvatar.propTypes = {
  classes: PropTypes.object.isRequired,
  name: PropTypes.string.isRequired,
  image: PropTypes.string,
  subtitle: PropTypes.string
};

customAvatar.defaultProps = {
  name: "John Doe",
  subtitle: ""
};

export default withStyles(styles)(customAvatar);

The Avatar component is the child component of a parent, the code that follows is how the Avatar component is used in the parent. Avatar组件是父组件的子组件,下面的代码是Avatar组件在父组件中的使用方式。

import React from "react";
import PropTypes from "prop-types";
import {
  withStyles,
  Card,
  CardContent
} from "@material-ui/core";
import AvatarProfile from "./AvatarProfile";

const styles = {
  cardContent: {
  },
  AvatarDiv: {
    backgroundColor: "red"
  }
};

const ItemCardWithCheckbox = props => {
  const { classes, name, subtitle } = props;

  return (
    <Card>
      <CardContent className={classes.cardContent}>
        <AvatarProfile
          name={name}
          subtitle={subtitle}
          className={classes.AvatarDiv}
        />
      </CardContent>
    </Card>
  );
};

ItemCardWithCheckbox.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles(styles)(ItemCardWithCheckbox);

As you can see I am trying to apply the AvatarDiv style for the Avatar component ie, I would like the backgroundColor of the Avatar component to be red but that's not happening, I am using Material UI .如您所见,我正在尝试为Avatar组件应用AvatarDiv样式,即,我希望Avatar组件的backgroundColor为红色,但没有发生,我正在使用Material UI My guess is either the style props aren't being passed properly to the Avatar component or I'm not applying the style correctly.我的猜测是样式道具没有正确传递到Avatar组件,或者我没有正确应用样式。

You are passing AvatarDiv as 'className' prop.您正在将 AvatarDiv 作为“className”道具传递。 You are not applying the class here, as AvatarDiv is a custom component.您没有在此处应用该类,因为 AvatarDiv 是一个自定义组件。

  <AvatarProfile
          name={name}
          subtitle={subtitle}
          className={classes.AvatarDiv}

You have to pass it as props and use that prop to apply the style in the child component.您必须将其作为道具传递并使用该道具在子组件中应用样式。 I have done something similar in the codesandbox, where i change the color to orange for the button and pass it as prop to child component.我在 codesandbox 中做了类似的事情,我将按钮的颜色更改为橙色,并将其作为 prop 传递给子组件。 Please check - https://codesandbox.io/s/lyxz92m7nl请检查 - https://codesandbox.io/s/lyxz92m7nl

I have made it more readable and smart component and use a single stlyles component.我使它更具可读性和智能组件,并使用单个 stlyles 组件。 Here is the working codesandbox: https://codesandbox.io/s/ll507vypy7这是工作代码和框: https ://codesandbox.io/s/ll507vypy7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM