How to perform type checking using prop-types in React?

Validate props in React components using the prop-types library to catch bugs early.

intermediatePerformance optimizationreactprop-typesvalidationcomponent safety
Published: 10/26/2025
Updated: 10/26/2025

Question

How to perform type checking using prop-types in React?


Answer

Install the prop-types package and define types on your component props.

npm install prop-types
import PropTypes from 'prop-types';

function UserCard({ name, age }) {
  return (
    <div>
      <h3>{name}</h3>
      <p>Age: {age}</p>
    </div>
  );
}

UserCard.propTypes = {
  name: PropTypes.string.isRequired,
  age: PropTypes.number.isRequired,
};

✅ If the prop type is incorrect or missing, React warns you in the console.

Real-World Example

  • Validating data in reusable components
  • Avoiding silent runtime bugs
  • Early catching of integration issues

Quick Practice

Add PropTypes.arrayOf(PropTypes.object) validation to a List component that accepts an array of items.

Summary

PropTypes help catch invalid props at runtime — useful for development and shared UI components.

Related Videos
Watch these videos to learn more about this topic
Frequently Asked Questions

Do PropTypes work in production?

They’re mainly for development checks; TypeScript is preferred for compile-time safety.


Stay Updated

Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.