JoeTidee JoeTidee - 1 month ago 6
React JSX Question

In React, how do you use propTypes to check the properties of an object?

I am passing props to a component in this way:

let obj = {
id: someId,
name: someName
};

<SomeComponent obj={obj} />


I want to be able to use
propTypes
to check the props entering into
SomeComponent
, for example:

SomeComponent.propTypes = {
obj.id: PropTypes.number,
obj.name: PropTypes.string
};


but the above syntax is not accepted - how is this accomplished?

Answer

Use PropTypes.shape for this purpose.

SomeComponent.propTypes = {
    obj : React.Proptypes.shape({
       id: React.PropTypes.number,
       name: React.PropTypes.string
    })
}