javascript - 如何将自定义属性传递给功能组件中的样式组件?
问题描述
我试图了解这件事是如何工作的,但我发现的所有示例都是以课堂方式编写的。
import React from 'react'
import styled from 'styled-components/native'
const MyTag = styled.Button.attrs({
title: myThisThingValue
})`
// some style
background: thisIsAlsoMyThingValue
\`
export default function MyComponent({ props }) {
return(
<MyTag myThisThing="My value" thisIsAlsoMyThing="Other Value" />
)
}
我只想访问 MyTag 样式中的自定义属性。我(props) => {title: props.MyThing }
在 .attrs() 中使用了一个但没有用。
解决方案
这应该工作:
JavaScript 版本:
export const MyTag = styled.button.attrs(props => ({
title: props.myThisThingValue,
}))`
background: ${props => props.thisIsAlsoMyThing};
`;
打字稿版本:
interface MyTagProps {
myThisThingValue: string;
thisIsAlsoMyThing: string;
}
export const MyTag = styled.button.attrs((props: MyTagProps) => ({
title: props.myThisThingValue,
}))<MyTagProps>`
background: ${props => props.thisIsAlsoMyThing};
`;
用它:
<MyTag myThisThingValue="My value" thisIsAlsoMyThing="red" />
推荐阅读
- python - 使用字典来乘以引用 Pandas 之类的列
- c++ - 通过外部分配的数据调用 Eigen GEMM
- java - JaCoCo 命令行界面 (CLI) 报告生成导致: 引起:java.lang.IllegalStateException:$CLASS 类已被检测
- amazon-web-services - AWS Elastic Beanstalk CloudFormation 脚本:无法在没有设置凭证的情况下签署请求
- arrays - 错误运行时 9
- python - 如何在没有 Python re 库的情况下替换字符串中的字符
- etl - icCube - ETL - 创建文件转储,然后将其用作数据源
- ios - 如何在 UITableView 中的两个单元格之间添加间距?没有像分隔符高度之类的东西
- if-statement - ksh if 循环 vs [[ ]] && { cmd1 ; } || { cmd2 ; }
- javascript - TensorFlowJS 的简单线性回归