【发布时间】:2021-06-27 16:07:23
【问题描述】:
在styled-components 中,我试图通过content 传递一个React 图标以在悬停时渲染它,但由于某种原因,我在悬停时的渲染是[Object Object]。
组件:
export const FooLink = styled(Link)`
padding: 0.5rem 2rem;
color: ${({ theme }) => theme.colors.white};
text-align: center;
margin: 0 auto;
position: relative;
font-size: ${({ theme }) => theme.fontSizes.p};
letter-spacing: 0.15em;
transition: ${({ theme }) => theme.animations.trans3};
&:after {
content: '${FaArrowRight}';
/* content: '>'; */
position: absolute;
opacity: 0;
right: -${({ theme }) => theme.spacings.small};
transition: ${({ theme }) => theme.animations.trans3};
}
&:hover {
padding-right: ${({ theme }) => theme.spacings.small};
padding-left: ${({ theme }) => theme.spacings.xxSmall};
}
&:hover:after {
opacity: 1;
${({ theme }) => theme.spacings.xSmall};
}
`
我把所有东西都带进来了:
import styled from 'styled-components'
import { Link } from 'gatsby'
import { FaArrowRight } from 'react-icons/fa'
对内容的其他尝试
content: ${FaArrowRight};
但我发现这不起作用:
这是因为内容值必须在 CSS 中的引号内。
意识到我可能在 CSS 心理状态中花费了太长时间,我尝试引入 React:
import React from 'react'
import styled from 'styled-components'
import { Link } from 'gatsby'
import { FaArrowRight } from 'react-icons/fa'
和渲染:
content: '${(<FaArrowRight />)}';
当我尝试使用模板文字时,我收到一个缺少分号的错误:
content: `'${<FaArrowRight />}'`;
所有尝试都呈现为[Object Object]
研究一下是否有人问过这个问题并且我已经阅读了:
- How to render pseudo before content dynamically in styled-component
- Before and After pseudo classes used with styled-components
- css pseudo code “li::before” in react inline style
- How do I get a variable from a graphQl query into a pseudo element in either inline styles or styled components
- Do psuedo selectors work in styled-components the same as in css with unicode characters
在styled-components 中,如何在content 中呈现 React 图标?
【问题讨论】:
-
在
:after之前粘贴& -
你这是一个编辑错误,但即便如此它仍然呈现为
object object -
'${FaArrowRight}'使用反引号而不是单引号。 -
@TomBombadil 如问题中所述,根据我的研究,单引号应与
content一起使用,而不是模板文字。 -
据我所知,您不能将对象作为值传递给内容。而且我认为您正在传递一个对象。不确定
FaArrowRight是字符串还是对象。
标签: javascript css reactjs styled-components react-component