Better Prop Validation in React

const { object, array } = React.PropTypesMeeting.propTypes = {
info: object.isRequired,
participants: array.isRequired,
}
const { shape, number, object, arrayOf } = React.PropTypesMeeting.propTypes = {
info: shape({
timestamp: number.isRequired,
room: number,
organizer: object,
}).isRequired,
participants: arrayOf(object).isRequired,
}
const { shape, number, string, arrayOf } = React.PropTypesMeeting.propTypes = {
info: shape({
timestamp: number.isRequired,
room: number,
organizer: shape({
id: number.isRequired,
name: string,
}),
}).isRequired,
participants: arrayOf(shape({
id: number.isRequired,
name: string,
})).isRequired,
}
const { number, string, shape, arrayOf} = React.PropTypesconst coworker = shape({
id: number.isRequired,
name: string,
})
Meeting.propTypes = {
info: shape({
timestamp: number.isRequired,
room: number,
organizer: coworker,
}).isRequired,
participants: arrayOf(coworker).isRequired,
}
const { number, string, shape, objectOf} = React.PropTypesconst coworker = shape({
id: number.isRequired,
name: string,
})
Meeting.propTypes = {
info: shape({
timestamp: number.isRequired,
room: number,
organizer: coworker,
}).isRequired,
participants: objectOf(coworker).isRequired,
}
{
1: coworker,
2: coworker,
'someKey': coworker,
...
}

--

--

--

Software developer @travelperk

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Moe Sattler

Moe Sattler

Software developer @travelperk

More from Medium

Under the Hood of React useEffect Dependencies

React 18. What’s new?

A Quick How-To Create React App with TypeScript