Igor Martins
Igor Martins

Reputation: 2047

Pass props to child component - React Native

I want to pass props color to a icon child. This <Feather /> I want to add color as props

This is my component and the Feather 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>
    )
  }
}

This is my component where I send props thas show in ThouchableOpacity

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

How can pass color in this props and it only show on icon?

Upvotes: 3

Views: 6216

Answers (1)

Tholle
Tholle

Reputation: 112777

You could use a prop called color for your CloseButton component that you pass to the Feather component.

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>
    )
  }
}

Usage

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

Upvotes: 1

Related Questions