如何从 graphQl 查询中获取变量到内联样式或样式化组件中的伪元素

How do I get a variable from a graphQl query into a pseudo element in either inline styles or styled components

我有一个似乎无法解决的难题 我正在从 DatoCMS 的 graphQL 数据库中查询颜色,并想在我的 Gatsby js 应用程序中更改伪元素的颜色 我可以像这样使用常规选择器

<p style={{color: pricing.packageBorderColor.hex}} className="price-session">
   <span>${pricing.priceAmount}</span> | <span>{pricing.lengthOfSession}</span>
</p>

但是我不确定如何将 :after 这样的 sudo 选择器引入组合中。

const ListItem = styled.li`
  list-style-type: none;
  font-size: 20px;
  display: inline-block;
  width: 330px;
  &:before {
    content: url(data:image/svg+xml,${encodeURIComponent(renderToString(<FontAwesomeIcon icon={faCheck} />))});
    width: 20px;
    display: block;
    float: left;
    position: absolute;
    margin-left: -31px;
    color: {pricing.packageBorderColor.hex} // This is what I'd ideally like to do, but doesnt seem doable
  }
  span{
    display:block;
    float:left; 
    margin-top:3px;
  }
`

我想可能是样式化组件并且这可行,但是我无法添加我的变量,因为样式化组件似乎存在于我的循环和反应组件之前的范围之外。

更新尝试

const CircleSave = styled.div`
  &:after{
    background: ({color}) => color
  }

`

<CircleSave color={pricing.packageBorderColor.hex} className="circle-save">
   <p>${pricing.packageSavings}</p>
   <p>savings</p>
 </CircleSave>

我在渲染中收到以下错误 css

.chrVyZ:after {
    background: ({color}) => color;
}

你可以使用styled components passed props来传递这样的道具:

const ListItem = styled.li`
  list-style-type: none;
  font-size: 20px;
  display: inline-block;
  width: 330px;
  &:before {
    content: url(data:image/svg+xml,${encodeURIComponent(renderToString(<FontAwesomeIcon icon={faCheck} />))});
    width: 20px;
    display: block;
    float: left;
    position: absolute;
    margin-left: -31px;
    color: ${({ color }) => color}; // This is what I'd ideally like to do, but doesnt seem doable
  }
  span{
    display:block;
    float:left; 
    margin-top:3px;
  }
`

然后像使用颜色道具的普通组件一样使用它:

<ListItem color={pricing.packageBorderColor.hex}/>