← Back to React Course | Chapter 3: Components & Props | Lesson 6 of 11

Type-Checking Props with PropTypes

PropTypes are like a checklist that warns you if you handed a component the wrong kind of ingredient.

Declaring PropTypes

After importing PropTypes from the prop-types package, you attach a propTypes object to your component listing the expected type of each prop. During development, React checks incoming props against this and logs a warning if something doesn't match.

Note: Mark props that must always be provided with .isRequired, like PropTypes.string.isRequired.

Warning: PropTypes warnings only appear in the browser console -- they never stop your app from rendering.

Example: Declaring PropTypes

markup
// Run in your local React project (npm install required)
import PropTypes from 'prop-types';

function Greeting({ name, age }) {
  return <p>{name} is {age} years old.</p>;
}

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

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Common PropTypes Validators

The prop-types library provides validators for all common JavaScript types -- string, number, bool, array, object, func -- plus more advanced ones like PropTypes.oneOf for a fixed set of allowed values. This lets you be precise about exactly what shape of data a component expects.

Note: Use PropTypes.oneOf([...]) for props that should only ever be one of a specific list of strings, like a size prop.

Warning: PropTypes checks run only in development mode -- they are stripped out of production builds for performance.

Example: Common PropTypes Validators

markup
// Run in your local React project (npm install required)
import PropTypes from 'prop-types';

function StatusBadge({ status }) {
  return <span className={`badge-${status}`}>{status}</span>;
}

StatusBadge.propTypes = {
  status: PropTypes.oneOf(['active', 'inactive', 'pending']).isRequired,
};

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

PropTypes vs TypeScript

PropTypes checks prop types while the app is running in the browser, whereas TypeScript checks types while you're writing the code, before it even runs. TypeScript catches more mistakes earlier, which is why many modern React projects prefer it over PropTypes.

Note: If your project already uses TypeScript, you generally don't need PropTypes too -- TypeScript's types cover the same need at compile time.

Warning: Switching from PropTypes to TypeScript requires rewriting prop declarations in a different syntax -- they aren't interchangeable line-for-line.

Example: PropTypes vs TypeScript

markup
// Run in your local React project (npm install required)
// PropTypes (runtime check):
Greeting.propTypes = { name: PropTypes.string };

// TypeScript (compile-time check) equivalent:
type GreetingProps = { name: string };
function Greeting({ name }: GreetingProps) {
  return <p>Hello, {name}</p>;
}
Common Mistakes
  1. Forgetting to install and import the separate prop-types package -- it's not built into React itself.
  2. Using PropTypes and expecting them to stop the app from running -- they only log warnings, they don't block anything.
  3. Confusing PropTypes (runtime checking) with TypeScript (compile-time checking) -- they solve a similar problem differently.
Chapter Summary
  • PropTypes let you declare the expected type of each prop a component receives.
  • They come from the separate prop-types npm package, not React core.
  • Mismatched prop types produce a console warning in development, not an error.
  • Many modern projects use TypeScript instead of PropTypes for stronger type safety.
Browser Support

No browser-specific restrictions -- prop-types works in any React version but must be installed as a separate npm package.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.