reactjs - 为什么 styled-components 没有为整个组件设置样式?
问题描述
我是使用 react 来设计组件样式的新手,到目前为止,我是这样使用它的:
const Wrapper=styled.ul`//这里所有的css`` 这里的所有 css 代码
但在这种情况下,我想为整个组件链接设置样式,而不必像这样包装它:
const StyledLinks = styled(links)`//这里所有的css`
为了检查它是否正常工作,我用它绘制了文本,color:red
但它似乎不起作用,没有应用任何样式。我想这是非常愚蠢的事情,但非常感谢您的帮助!
谢谢!
import React from "react"
import { Link } from "gatsby"
import styled from "styled-components"
const links = () => {
return (
<ul >
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/blog">Blog</Link>
</li>
</ul>
)
}
const StyledLinks = styled(links)`
color: red;
display: flex;
justify-content: space-around;
width: 100%;
a {
text-decoration: none;
color: red;
}
`
export default StyledLinks
解决方案
我不得不重新写这个。哈哈。这是它应该看起来的样子:
import React from "react"
import { Link } from "gatsby"
import styled from "styled-components"
const Links = () => {
return (
<Ul >
<li>
<StyledLinks to="/">Home</StyledLinks>
</li>
<li>
<StyledLinks to="/about">About</StyledLinks>
</li>
<li>
<StyledLinks to="/blog">Blog</StyledLinks>
</li>
</Ul>
)
}
const StyledLinks = styled(Link)`
color: red;
display: flex;
justify-content: space-around;
width: 100%;
text-decoration: none;
&:hover {
text-decoration: underline;
}
`;
const Ul = styled.ul`
list-style:none;
`;
export default Links
推荐阅读
- python - Django查询获取不在其他对象中的对象
- spring - 如何修复春季批处理中没有正在进行的交易
- reactjs - 使用 Jest/Enzyme 测试 Chart.js - 无法创建图表:无法从给定项目获取上下文
- javascript - 将表1的一个tr拖放到表2的一个tr上
- c# - 如何以编程方式将 .MDF 文件附加到 Azure SQL Server c#
- sql - 使用复杂的 IIF 条件语句时出现表达式太复杂的错误 - Access SQL
- c# - LINQ 连接谓词 OR
- eclipse - 如何将 Eclipse 从 2018-09 更新到 2018-12
- pandas - 熊猫数据框合并的问题
- c - 为什么我的 Xcode 找不到我创建的 .txt 文件?