reactjs - React,Styled-component:覆盖子组件样式
问题描述
我想覆盖 Button 组件样式,但它不起作用。您能告诉我如何覆盖它,还是应该将所有样式都赋予子按钮组件?
按钮.js
const StyledButton = styled.button`
color: white;
background-color: yellow;
`;
const Button = ({text}) => {
return <StyledButton>{text}</StyledButton>;
};
export default Button;
父.js
import Button from "./Button";
const OverrideButton = styled(Button)`
&& {
color: blue;
background-color: green;
}
`;
const Parent = () => {
return <OverrideButton text="Parent" />;
};
解决方案
您的课程已创建但未应用。你需要把它传下去。
const Button = ({text, className}) => {
return <StyledButton className={className}>{text}</StyledButton>;
};
在这里得到了回答:https ://stackoverflow.com/a/54113434/12959556
示例:https ://stackblitz.com/edit/react-a5mdpo?embed=1&file=index.js
推荐阅读
- loss - Keras 自定义损失函数以与 y_true 和 y_pred 相同的批量大小传递参数(包含已学习噪声的 numpy 数组)
- r - 如何叠加密度图ggplot?
- javascript - 没有输入字段的 Tempus Dominus
- python - 如何在不评估的情况下将分数保留在输出中
- python - 使用 Keras 从短信中挑选有用的价值
- python - 不能使用 pypi 或 pip
- json - 通过存储在 S3 中的 AWS Athena 读取相同格式的 JSON 和 CSV
- angular - 我的 Springboot 没有接到电话
- netsuite - Netsuite 新手:引起您对 netsuite 的关注
- python - 如何在 Pandas 中创建动态数据框