gpt4 book ai didi

javascript - 将 Prop 传递给子组件 - React Native

转载 作者:行者123 更新时间:2023-11-29 15:13:41 24 4
gpt4 key购买 nike

我要传递 Prop color给一个图标 child 。这<Feather />我想添加 color作为 Prop

这是我的组件和羽毛 child

import { Feather } from '@expo/vector-icons'

export default class CloseButton extends React.PureComponent {
render () {
const { style, ...props } = this.props
return (
<TouchableOpacity style={styles.close} link {...props} >
<Feather name='x' size={26} /> // <-- want add color here
</TouchableOpacity>
)
}
}

这是我发送在 ThouchableOpacity 中显示的 Prop 的组件

<Alert.CloseButton onPress={props.onRequestClose} />

如何在这个 Prop 中传递颜色并且它只显示在图标上?

最佳答案

您可以为传递给 Feather 组件的 CloseButton 组件使用名为 color 的 Prop 。

export default class CloseButton extends React.PureComponent {
render () {
const { style, color, ...props } = this.props;

return (
<TouchableOpacity style={styles.close} link {...props} >
<Feather name='x' size={26} color={color} />
</TouchableOpacity>
)
}
}

用法

<Alert.CloseButton
onPress={props.onRequestClose}
color="red"
/>

关于javascript - 将 Prop 传递给子组件 - React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51676397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com