reactjs - 如何在 react 和 typescript 中使用样式化组件重构代码?
问题描述
我想使用 react 和 typescript 重构样式组件的代码。
我有两个黑色和蓝色链接共享相同的 css,但有些风格不同。
下面是代码,
return (
<Wrapper>
<ButtonLink a="someurl">
black
</ButtonLink>
<ButtonLink a="url">
blue
</ButtonLink>
</Wrapper>
);
const ButtonLink = styled.a`
border: none;
background: none;
display: flex;
justify-content: center;
align-items: center;
`;
现在对于黑色链接,我想添加背景颜色:黑色,对于蓝色链接,我想添加背景颜色蓝色。
如何使用样式组件将这些样式添加到这两个链接中。有人可以帮我解决这个问题。谢谢。
解决方案
例如,您需要将属性作为道具传递给组件
<ButtonLink color="red" href="#">Red</ButtonLink>
const ButtonLink = styled.a`
...,
color: ${props => props.color}
`
推荐阅读
- excel - 寻求更有效的公式从分隔文本字符串中提取字段
- android-studio - Kotlin (Android Studio) 意外标记(使用 ';' 分隔同一行上的表达式)
- javascript - 在 javascript V8 中,编译阶段是否发生在执行阶段之前的函数,然后所有代码都被执行或仅用于全局上下文
- node.js - Sequelize - 使用嵌套包含创建确实返回嵌套对象,但不会在 DB 中创建它
- windows - windows cmd set如何解析变量名?
- javascript - 链接中的普塔价值
- docker - 我在 `docker ps` 中看到了一些未知的容器
- c# - 为什么除非我们使用块,否则以下表达式树不会编译?
- java - 使用 setter(或任何东西)时,数组的对象显示错误“Java 返回:1”
- spring-integration - EDI 的 Spring 集成