在 React 风格的组件中使用 props

Using props inside react styled components

我正在使用 React styled-components 库来设计我的应用程序并偶然发现一个问题,我无法根据传递给组件的道具分配 CSS 属性。

import React from 'react'
import styled from 'styled-components'

const Bar = styled.div`
  width: ${props => props.fraction}px;
`
const bar = (props) => {
return (
  <Bar>{props.fraction}</Bar>
  )
};

export default bar

分数道具从父组件传递并在样式组件内完美输出,但样式中分配的宽度 属性 在开发人员工具中交叉,那里只有 px,所以分数甚至没有到达那里.

如有任何帮助,我将不胜感激! 非常感谢

您应该将 fraction 作为 属性 传递给 Bar 组件,如下所示:

cost BarComponent = ({ fraction }) => (
  <Bar fraction={fraction}>{fraction}</Bar>
);