Reputation: 26054
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?
Upvotes: 0
Views: 189
Reputation: 7308
Use PropTypes.shape for this purpose.
SomeComponent.propTypes = {
obj : React.Proptypes.shape({
id: React.PropTypes.number,
name: React.PropTypes.string
})
}
Upvotes: 3
Reputation: 11807
Wouldn't this work?
SomeComponent.propTypes = {
obj: React.PropTypes.shape({
id: PropTypes.number,
name: PropTypes.name
})
};
Upvotes: 1