PropTypes
Summary
Runtime type checking for React props and similar objects.
Overview
As your app grows, you can catch a lot of bugs with typechecking. For some applications, you can use JavaScript extensions like Flow or TypeScript to typecheck your whole application.
But even if you don’t use those, the React.PropTypes library offers some crucial typechecking abilities.
PropTypes was originally exposed as part of the React core module, and is commonly used with React components to type check props.
React.PropTypes has moved into a different package since React v15.5. Please use the prop-types library instead.
Installation
npm install --save prop-types
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Learning React</title>
</head>
<body>
<div id="root"></div>
<script src="/node_modules/react/umd/react.development.js"></script>
<script src="/node_modules/react-dom/umd/react-dom.development.js"></script>
+ <script src="/node_modules/prop-types/prop-types.js"></script>
</body>
</html>
In Create React App, where you would be using ES Modules you will need to
import PropTypes from "prop-types;"but you do not need to in this demonstration.
Usage
Add the following code with the
propTypesdeclaration commented out and don't pass the requirednameprop as shown below:main.jsfunction Greeter(props) {
return <h1>Hello, {props.name}</h1>;
}
// Greeter.propTypes = {
// name: PropTypes.string.isRequired,
// };
const element = <Greeter />;
ReactDOM.createRoot(document.getElementById("root")).render(element);Notice it runs without an error.
Uncomment the
propTypesdeclaration.main.jsfunction Greeter(props) {
return <h1>Hello, {props.name}</h1>;
}
Greeter.propTypes = {
name: PropTypes.string.isRequired,
};
const element = <Greeter name="Joe" />;
ReactDOM.createRoot(document.getElementById("root")).render(element);Notice you receive a helpful warning message in the console.
Pass the
nameproperty the stringSrini.main.jsfunction Greeter(props) {
return <h1>Hello, {props.name}</h1>;
}
Greeter.propTypes = {
name: PropTypes.string.isRequired,
};
const element = <Greeter name="Srini" />;
ReactDOM.createRoot(document.getElementById("root")).render(element);Notice the code works and runs without any warning messages.
Pass the
nameproperty a number.main.js...
const element = <Greeter name={1} />;
...Notice you receive a helpful warning message in the console.