Cri*_*ano 5 javascript reactjs react-native react-native-android styled-components
我试图了解这件事是如何工作的,但我发现的所有示例都是以课堂方式编写的。
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" />
)
}
Run Code Online (Sandbox Code Playgroud)
我只想访问 MyTag 样式中的自定义属性。我(props) => {title: props.MyThing }在 .attrs() 中使用了 a但没有用。
小智 7
这应该有效:
JavaScript 版本:
export const MyTag = styled.button.attrs(props => ({
title: props.myThisThingValue,
}))`
background: ${props => props.thisIsAlsoMyThing};
`;
Run Code Online (Sandbox Code Playgroud)
打字稿版本:
interface MyTagProps {
myThisThingValue: string;
thisIsAlsoMyThing: string;
}
export const MyTag = styled.button.attrs((props: MyTagProps) => ({
title: props.myThisThingValue,
}))<MyTagProps>`
background: ${props => props.thisIsAlsoMyThing};
`;
Run Code Online (Sandbox Code Playgroud)
用它:
<MyTag myThisThingValue="My value" thisIsAlsoMyThing="red" />
Run Code Online (Sandbox Code Playgroud)
Nic*_*wer -3
您将为各个属性创建函数,而不是为整个 attrs 参数创建函数。例如:
const MyTag = styled.Button.attrs({
title: (props) => props.myThisThing
})`
background: ${props => props.thisIsAlsoMyThing}
`
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8761 次 |
| 最近记录: |