简体   繁体   English

在内部使用道具来响应样式化的组件

[英]Using props inside react styled components

I am using React styled-components library to style my application and stumbled on a problem, that I can not assign a CSS property based on props passed to component. 我正在使用React样式组件库来设置我的应用程序的样式,并偶然发现了一个问题,即我无法基于传递给组件的道具分配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

Fraction props is passed from parent component and outputs perfectly inside the styled component, but the width property assigned in styles is crossed in developer tools having only px there, so the fraction number does not even get there. 分数道具从父组件传递并在样式化的组件内完美输出,但是样式分配的width属性在只有px的开发人员工具中被交叉,因此分数号甚至没有到达那里。

I would appreciate any help! 我将不胜感激任何帮助! Thanks a lot 非常感谢

You should pass fraction as a property to Bar component, like this: 您应该将fraction作为属性传递给Bar组件,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM