Adam Adam - 1 year ago 259
React JSX Question

How do you validate the PropTypes of a nested object in ReactJS?

I'm using a data object as my props for a component in ReactJS.

<Field data={data} />

I know its easy to validate the PropTypes object itself:

propTypes: {
data: React.PropTypes.object

But what if I want to validate the values inside? ie., data.title?

props[propName]: React.PropTypes.number.required // etc...

Answer Source

You can use React.PropTypes.shape to validate properties:

propTypes: {
    data: React.PropTypes.shape({
      id: React.PropTypes.number.isRequired,
      title: React.PropTypes.string
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download