【发布时间】:2020-02-01 20:49:30
【问题描述】:
const Shape = styled.div`
background-color: ${props => props.bgColor || 'red'};
`
function shape(props) {
console.log(props.bgColor || 'red')
return (
<Shape>
</Shape>
)
}
我通过扩展运算符传递以下道具。
let square2x2 = { bgColor: "blue"}
<Shape {...square2x2}></Shape>
console.log 显示 props.bgColor 存在并返回蓝色。但是该元素仍然是红色的。我还通过 React 开发工具确认了 props 的存在。我在哪里搞砸了?
编辑:解决方案,使用 <Shape {...props}/> 将道具传递给 Shape
【问题讨论】:
-
你将 props 传递给
shape,但这不会将 props 传递给Shape...
标签: javascript reactjs styled-components