javascript - Styled-components 道具被传递但不适应
问题描述
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<Shape {...props}/>
解决方案
道具也必须传递给 Shape:
function shape(props) {
return (
<Shape bgColor={props.bgColor} />
)
}
您需要了解,当您将其传递给shape
您时,您仍然不在样式化的 div 对象中,实例化组件的样式化 div 对象在Shape
里面shape
,里面的道具在shape
范围之外Shape
,所以您必须将它们传递给Shape
to。
推荐阅读
- javascript - 使用初始状态反应 useState 钩子事件处理程序
- python - NameError: X while execution WebLogic Scripting Tool (WLST) script
- pandas - Shift 发生:使用 pandas shift 来组合行
- jms - 使用 Spring、ActiveMQ 和 Qpid 进行集成测试
- kotlin - 将 kotlin 中的扩展功能添加到所有类
- html - 响应式文本定位?
- c# - 多次打开 WPF 应用程序
- acumatica - Acumatica REST API - StockItem - 如何在请求中使用多个产品 ID
- nginx - 例程:SSL23_GET_SERVER_HELLO:尝试使用代理连接到 Neo4j 服务器时的未知协议
- javascript - 在没有任何 PHP 的情况下刷新 HTML 中的 div?