【问题标题】:How to pass prop to Material-UI component using styled-components如何使用 styled-components 将 prop 传递给 Material-UI 组件
【发布时间】:2021-05-19 18:46:35
【问题描述】:

我想使用样式组件设置 MUI 按钮的样式。我想将variant='outlined' 作为组件传递给组件。这是我正在尝试的:

export const StyledButton = styled(Button).attrs(() => ({
  variant: 'outlined',
}))

它会抛出这个错误:

【问题讨论】:

标签: reactjs material-ui styled-components


【解决方案1】:

要传递属性,您必须执行以下操作:

const StyledButton = styled(Button)`
  background-color: #6772e5;
  color: #fff;
  box-shadow: 0 4px 6px rgba(50, 50, 93, 0.11), 0 1px 3px rgba(0, 0, 0, 0.08);
  padding: 7px 14px;
  &:hover {
    background-color: #5469d4;
  }
`;

export default function StyledComponent() {
  return (
      <StyledButton variant="outlined">Customized</StyledButton>
  );
}

更多内容可以参考官方Doc

【讨论】:

    猜你喜欢
    • 2021-10-13
    • 2020-05-28
    • 2017-12-21
    • 2019-08-02
    • 2018-09-14
    • 2021-05-25
    • 2020-05-06
    • 2019-06-27
    • 2021-11-25
    相关资源
    最近更新 更多