David Weldon
David Weldon

Reputation: 64342

React prop validation for date objects

What is the currently preferred way to validate a Date prop in react?

Right now I'm using: React.PropTypes.object

This, however, is now failing the forbid-prop-types lint rule. Should I use a shape or is there some better way?

Upvotes: 285

Views: 104855

Answers (2)

Sachin Som
Sachin Som

Reputation: 1201

This worked for me

PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
    PropTypes.instanceOf(Date),
]).isRequired

Upvotes: 2

Alex Mcp
Alex Mcp

Reputation: 19315

Pretty sure you could use PropTypes.instanceOf(Date)

Upvotes: 632

Related Questions