javascript - 使用 Emotion.sh 从样式化组件传递道具
问题描述
我有以下使用声明的样式按钮@emotion\styled
:
const Button = styled.button`
background: blue;
${size({
width: props => props.width
)}
`
在我的代码中,size
是一个基于传递的道具返回大小 css 序列化样式的函数:
const size = ({ width: "m"} = {}) => {
switch(width) {
case "s":
return css`width: 100px`;
break;
case "m":
return css`width: 150px`;
break;
}
}
<Button width="s">Some text</Button>
但是,width
我在函数中收到的值size
不是来自传递的 prop ie 的解析值s
,而是一个 string props => props.width
。
有没有办法将道具从类似于我想要实现的样式组件传递到函数中?
解决方案
它不起作用,因为您将匿名函数传递给width
, 在您不期望函数的地方。
我很确定这是你想要做的:
const Button = styled.button`
background: blue;
${props => size({ width: props.width })}
`
这样,您将在样式声明的范围内传递一个匿名函数,从 Emotion 获取道具并随意使用它。
推荐阅读
- spring-boot - 使用千分尺时的 Prometheus 端点错误
- asp.net - 页面生成以适应列表溢出
- android - Android位置:绝对不应该在有边框的容器内被切断
- azure-active-directory - 通过应用注册门户创建的应用在 Azure Active Directory->应用注册列表中不可见
- angular - Angular 2 ngOnchange 事件被调用,与任何更改无关
- python - 如何将函数输出用作另一个函数的输入
- sbt - 如何指定包含空格的任务键名称?
- javascript - 在没有 FileUploader 的 SAPUI5 中上传图像
- json - 在按钮上从 json 更改数据单击 React-Native
- php - 如何从不同的数组中获取随机值