matt-p matt-p - 25 days ago 8
React JSX Question

React Proptypes - stateless components

<div className="dm-inbox-item__project">{props.message.project}</div>


I am trying to set out the propTypes within a stateless component, and wish to define propTypes for the properties on message. Currently, I have only been successful in defining the whole message.

InboxItem.propTypes = {
message: PropTypes.string.isRequired,
};


I would like to do something like the below in order to validate individually.

Inbox.propTypes = {
message.project: Proptypes.string.isRequired,
};


Any thoughts would be appreciated.

Answer

You can do that with React.PropTypes.shape.

Inbox.propTypes = {
    message: React.PropTypes.shape({
      project: React.PropTypes.string.isRequired
    })
}