Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of the '.isRequired' when defining static PropTypes in React-Native

static propTypes = {
  // Value to display
  value: PropTypes.string.isRequired,
  // Wheter this values was chosen by user or not
  isChosen: PropTypes.bool.isRequired,
  // Gets called when user choses this value
  onChoose: PropTypes.func.isRequired,
}

Why Should I add '.isRequired' in when defining the above PropTypes? I am a react-native beginer

like image 655
Isaac Sekamatte Avatar asked Oct 11 '25 19:10

Isaac Sekamatte


1 Answers

If you define the PropType with isRequired, React will warn you when you use that component without passing that prop.

// -> Warning: onChoose is required but not provided
<Component value={val} isChosen={true} /> 

If you do not define it, the property is considered optional and no warning is displayed if you don't pass the prop.

// -> No warnings, since the props are not required.
<Component /> 
like image 92
jevakallio Avatar answered Oct 14 '25 11:10

jevakallio