reactjs - 如何使用 styled-components 将 prop 传递给 Material-UI 组件

标签 reactjs material-ui styled-components

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

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

它抛出这个错误: enter image description here

最佳答案

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

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

关于reactjs - 如何使用 styled-components 将 prop 传递给 Material-UI 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66232468/

相关文章:

reactjs - 在react-testing-library中,渲染应该只被调用一次吗?

reactjs - 如何连接到 Metamask 或 Coinbase 钱包?

reactjs - Aria-hidden 不会从查询中隐藏元素

reactjs - Material UI Dialog 变成 Hook 闪烁

reactjs - MaterialUI for React with Styled-Components

jestjs - 开 Jest - `Cannot create styled-component for component: undefined`

reactjs - 防止带有样式组件的 Component/PureComponent 在状态更改后重新渲染所有项目

javascript - 如何使 flex-direction : column; properly. 我的代码不起作用

javascript - 映射数组并返回 API 结果

javascript - 如何在 react 中设置标签的背景颜色?