将道具传递给 child 组件 - React Native

Pass props to child component - React Native

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

这是我的组件和羽毛 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

中显示的道具的组件

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

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

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

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"
/>