Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 706
matt-p Avatar asked Feb 01 '26 23:02

matt-p


1 Answers

You can do that with React.PropTypes.shape.

Inbox.propTypes = {
    message: React.PropTypes.shape({
      project: React.PropTypes.string.isRequired
    })
}
like image 90
Grgur Avatar answered Feb 04 '26 14:02

Grgur