Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React prop validation for date objects

Tags:

reactjs

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?

like image 892
David Weldon Avatar asked Oct 04 '22 06:10

David Weldon


People also ask

How do you apply validation on props in React?

React JS has an inbuilt feature for validating props data type to make sure that values passed through props are valid. React components have a property called propTypes which is used to setup data type validation. Syntax: The syntax to use propTypes is shown below. class Component extends React.

Should you use PropTypes?

Props and PropTypes are important mechanisms for passing read-only attributes between React components. We can use React props, short for properties, to send data from one component to another. If a component receives the wrong type of props, it can cause bugs and unexpected errors in your app.

What is the use of PropTypes in React?

PropTypes are simply a mechanism that ensures that the passed value is of the correct datatype. This makes sure that we don't receive an error at the very end of our app by the console which might not be easy to deal with.

How do you use PropTypes in a functional component React?

import React from 'react'; import PropTypes from 'prop-types'; function List(props) { const todos = props. todos. map((todo, index) => (<li key={index}>{todo}</li>)); return (<ul></ul>); } List. PropTypes = { todos: PropTypes.


1 Answers

Pretty sure you could use PropTypes.instanceOf(Date)

like image 596
Alex Mcp Avatar answered Oct 20 '22 08:10

Alex Mcp