2

I need to know how to set a default value for props to React Native. I have the following code:

<View style={{ ...styles.textBox, ...props.bg }}>
    <Text style={{ ...styles.pretitle, ...props.style }}>{props.pretitle}</Text>
    <Text style={{ ...styles.title, ...props.style2 }}>{props.title}</Text>
    <Text style={styles.introduction}>{{ '', ...props.introduction }}</Text>
</View>

If props.introduction is 0 I want to set it to empty, like: '', or to have some other value, like: 'Not set'.

Thank you

SDushan
  • 4,341
  • 2
  • 16
  • 34
Korovjov
  • 503
  • 5
  • 17

1 Answers1

5

You can use defaultProps in React

export default class Example extends React.Component {
  render() {
    return (
      <View>
        <Text>{this.props.fName},{this.props.lName}</Text>
      </View>
    );
  }
}


Example.defaultProps = {
    fName: "Hello",
    lName: 'world'
}

Hope this helps you. Feel free for doubts.

SDushan
  • 4,341
  • 2
  • 16
  • 34