JoeTidee
JoeTidee

Reputation: 26054

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?

Upvotes: 0

Views: 189

Answers (2)

FurkanO
FurkanO

Reputation: 7308

Use PropTypes.shape for this purpose.

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

Upvotes: 3

james emanon
james emanon

Reputation: 11807

Wouldn't this work?

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

Upvotes: 1

Related Questions