Props Types
Learn how to document and validate the types of props passed to React components using PropTypes.
01 What Are PropTypes?
PropTypes is a runtime type-checking utility for React props. It helps developers identify when a component receives a value that does not match the expected type during development.
PropTypes checks whether supplied props match the declared types.
02 Installing PropTypes
In a React project that uses PropTypes, install the
prop-types package using the terminal.
npm install prop-types
After installation, import PropTypes into the
component file where you want to declare the expected types.
import PropTypes from "prop-types";
The prop-types package is added to the project dependencies.
Exact terminal messages may differ depending on the package manager and project.
03 Validating a String Prop
Use PropTypes.string when a prop is expected to
contain a string value.
import PropTypes from "prop-types";
function Student({ name }) {
return <h2>Student Name: {name}</h2>;
}
Student.propTypes = {
name: PropTypes.string
};
export default Student;
The name prop is expected to be a string. The
declaration does not automatically make the prop required.
Student Name: Samadhan
04 Validating Multiple Prop Types
PropTypes provides validators for common JavaScript data types, including strings, numbers and booleans.
import PropTypes from "prop-types";
function Student({ name, age, isEnrolled }) {
return (
<div>
<h2>{name}</h2>
<p>Age: {age}</p>
<p>Enrolled: {isEnrolled ? "Yes" : "No"}</p>
</div>
);
}
Student.propTypes = {
name: PropTypes.string,
age: PropTypes.number,
isEnrolled: PropTypes.bool
};
export default Student;
Samiksha
Age: 22
Enrolled: Yes
05 Making Props Required
Add .isRequired when a prop must be supplied.
If it is missing during development, PropTypes reports a warning.
import PropTypes from "prop-types";
function Student({ name, age }) {
return (
<div>
<h2>{name}</h2>
<p>Age: {age}</p>
</div>
);
}
Student.propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number.isRequired
};
export default Student;
Pradnya
Age: 23
Both required props are supplied in this example.
06 Validating an Array
Use PropTypes.array to check that a prop is an array.
Use PropTypes.arrayOf() to check the type of each item.
import PropTypes from "prop-types";
function Student({ skills }) {
return (
<ul>
{skills.map(skill => (
<li key={skill}>{skill}</li>
))}
</ul>
);
}
Student.propTypes = {
skills: PropTypes.arrayOf(PropTypes.string).isRequired
};
export default Student;
- HTML
- CSS
- JavaScript
07 Validating an Object
Use PropTypes.shape() to describe the expected
properties of an object prop.
import PropTypes from "prop-types";
function Student({ details }) {
return (
<div>
<h2>{details.name}</h2>
<p>Course: {details.course}</p>
</div>
);
}
Student.propTypes = {
details: PropTypes.shape({
name: PropTypes.string.isRequired,
course: PropTypes.string.isRequired
}).isRequired
};
export default Student;
Samadhan
Course: React JS
08 Validating Specific Values
Use PropTypes.oneOf() when a prop should match one
of a specific set of values.
import PropTypes from "prop-types";
function Course({ level }) {
return <p>Course Level: {level}</p>;
}
Course.propTypes = {
level: PropTypes.oneOf([
"Beginner",
"Intermediate",
"Advanced"
]).isRequired
};
export default Course;
Course Level: Intermediate
09 Default Props with PropTypes
PropTypes describes and checks expected types. Default values provide fallback data. In a functional component, you can specify defaults during parameter destructuring.
import PropTypes from "prop-types";
function Student({ name = "Guest" }) {
return <h2>Student Name: {name}</h2>;
}
Student.propTypes = {
name: PropTypes.string
};
export default Student;
Student Name: Guest
10 Complete PropTypes Example
This example validates a student's name, age and skills array in one reusable component.
import PropTypes from "prop-types";
function Student({ name, age, skills }) {
return (
<div>
<h2>{name}</h2>
<p>Age: {age}</p>
<h3>Skills
Example parent component:
import Student from "./Student";
function App() {
return (
<Student
name="Pradnya"
age={23}
skills={["React JS", "JavaScript", "CSS"]}
/>
);
}
export default App;
Pradnya
Age: 23
Skills
- React JS
- JavaScript
- CSS
Summary
PropTypes helps document and validate the expected types of props passed to React components during development. Common validators include string, number, bool, arrayOf, shape and oneOf. Use isRequired for props that should be supplied, and use default values separately when fallback data is needed. PropTypes improves code clarity and helps identify incorrect component inputs while building reusable interfaces at CIIT Training Institute.