Type checking a Component's Props - KatyaHorton/Udacity-React-practice GitHub Wiki

Type checking a Component's Props with PropsTypes

PropTypes is a package which helps us to avoid unintended (unexpected) data types, in our app, as it becomes bigger.

To install: npm install --save prop-types

Like if we accidentally pass an object, where array is expected, etc.

With PropTypes we can specify (if we wish so) the specific properties that we are passing into a component to be required (or not).

Our ListContacts.js example:

//imports PropTypes from the package we insatalled 
import PropTypes from 'prop-types'

...

ListContacts.proptypes = {
  contacts: PropTypes.array.isRequired,
  onDeletecontact:PropTypes.func.isRequired
}

Adds a property to our component, setting the value of

  • contacts to an array
  • onDeleteContact to a function, both PropTypes are required ;)