Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 132 ยท Components and Props

PropTypes, defaultProps and Runtime Prop Validation

Learn PropTypes validators, defaultProps, why React 19 removed both for function components, and the modern replacements - default parameter values, TypeScript and runtime schemas.

For years, every React tutorial taught two things right after props: PropTypes, to check that a component receives the right kinds of values, and defaultProps, to fill in the ones the caller leaves out. React 19 removed both for function components. Old code still contains them, older tutorials still teach them, and interviewers still ask about them, so you need to be able to read them, understand what they did, and replace them with the modern equivalents.

This lesson shows the old API, proves in the editor what React 19 does with it, and then gives you three current ways to get the same safety. The prop-types package cannot be installed in the editor, so the validators are shown read-only and a small hand-written checker (a stand-in) demonstrates the idea.

What PropTypes were for

JavaScript has no types, so nothing stops a caller from passing a string where you expected a number, or from forgetting a required prop. In development, React used to check Component.propTypes while rendering and print a console warning that named the component and the prop. You added the checks with the separate prop-types package:

JSX
// read-only: needs `npm install prop-types`
import PropTypes from 'prop-types';

function ProductCard({ name, price, tags, onBuy }) {
  return (
    <article>
      <h3>{name}</h3>
      <p>${price.toFixed(2)}</p>
      <ul>{tags.map((tag) => <li key={tag}>{tag}</li>)}</ul>
      <button onClick={onBuy}>Buy</button>
    </article>
  );
}

ProductCard.propTypes = {
  name: PropTypes.string.isRequired,
  price: PropTypes.number.isRequired,
  tags: PropTypes.arrayOf(PropTypes.string),
  onBuy: PropTypes.func,
};

Rendering <ProductCard name="Lamp" price="12" /> printed something like "Warning: Failed prop type: Invalid prop price of type string supplied to ProductCard, expected number." The component still rendered; PropTypes only warned, and only in development builds.

The validators

Every validator is a property of PropTypes. .isRequired can be chained to any of them.

Validator Accepts
PropTypes.string, number, bool, func, symbol, bigint A value of that primitive or function type
PropTypes.array, PropTypes.object Any array or object (no detail about contents)
PropTypes.node Anything React can render: strings, numbers, elements, arrays of them
PropTypes.element One React element, for example <Icon />
PropTypes.elementType A component type (a function, class or tag string)
PropTypes.instanceOf(Date) An instance of a class
PropTypes.oneOf(['sm', 'md', 'lg']) One of a fixed list of values (like an enum)
PropTypes.oneOfType([PropTypes.string, PropTypes.number]) Any of several types
PropTypes.arrayOf(PropTypes.number) An array whose items all match
PropTypes.objectOf(PropTypes.number) An object whose property values all match
PropTypes.shape({ id: PropTypes.number, name: PropTypes.string }) An object with at least these fields
PropTypes.exact({ id: PropTypes.number }) An object with exactly these fields, no extras
PropTypes.any Anything (use .isRequired to forbid undefined)
A custom function Your own rule, see below

A custom validator is a function (props, propName, componentName) => Error | null. Return an Error to report a problem:

JSX
// read-only
function percent(props, propName, componentName) {
  const value = props[propName];
  if (value == null) return null; // optional
  if (typeof value !== 'number' || value < 0 || value > 100) {
    return new Error(`${componentName}: ${propName} must be a number from 0 to 100`);
  }
  return null;
}

Meter.propTypes = { value: percent };

Seeing the idea run (a stand-in)

The validators are small functions: take a value, return an error message or nothing. Here is a miniature version, so you can see how little magic there is. It is not the real prop-types package.

A hand-written propTypes checker

The second card passes a string where a number was required, and the third forgets name, uses a size outside the allowed list and puts a number in a list of strings. Every problem is reported with the component and prop name, which is exactly the experience the real package gave.

What React 19 did

React 19 stopped checking propTypes: the property is ignored, and the prop-types package no longer has any effect on function components. It also removed defaultProps from function components. The React team's reasons were practical: PropTypes ran only in development, were easy to forget, and did nothing in production, while TypeScript, Flow and plain default parameters had become the common tools. defaultProps on class components still works.

You can verify both removals yourself:

React 19 ignores propTypes and function defaultProps

The function component prints Hello, Ada with no exclamation mark: its defaultProps was ignored (punctuation is undefined, which renders as nothing), and the failing propTypes rule never ran. The class component still gets its default. If you maintain a library that must work with React 18 and 19, keep the old API for 18 users and add the modern one next to it.

Modern replacements

1. Default parameter values

For defaults, use plain JavaScript default values in the destructuring pattern. They apply when the prop is undefined (not when it is null, which is a deliberate value):

JSX
function Button({ variant = 'primary', size = 'md', children }) {
  return <button className={`btn btn-${variant} btn-${size}`}>{children}</button>;
}

This is clearer than defaultProps because the default sits next to the code that uses it, and it works the same in every React version.

2. TypeScript (or JSDoc) for static checking

Types are checked while you write, in your editor and in CI, and they cost nothing at runtime. A .tsx version of the earlier card:

TSX
// read-only
type ProductCardProps = {
  name: string;
  price: number;
  size?: 'sm' | 'md' | 'lg';
  tags?: string[];
  onBuy?: () => void;
};

function ProductCard({ name, price, size = 'md', tags = [], onBuy }: ProductCardProps) {
  // ...
}

<ProductCard price="12" /> is now a compile error, with a message in the editor. The TypeScript with React lessons cover this in depth, and plain JavaScript projects can get much of it through JSDoc comments and // @ts-check.

3. Runtime schemas at the boundaries

TypeScript cannot check data that arrives from outside your code: JSON from an API, a URL parameter, a form. For those, validate at the boundary with a schema library such as Zod (see React Hook Form with Zod) and pass the trusted result into your components. You then do not need to validate props between your own components at all.

Migrating old code

  1. Delete Component.defaultProps for function components and move each default into the parameter list.
  2. Replace propTypes with TypeScript types (or JSDoc). Codemods exist: npx types-react-codemod and the prop-types-typescript generator can convert many files automatically.
  3. Search for components that read a default through the props object (for example props.size with a defaultProps entry) and give them an explicit default.
  4. Keep defaultProps on class components, or migrate those classes to functions as described in Class Components and the Lifecycle.

Common mistakes

  • Relying on PropTypes in React 19. They are silently ignored, so a "validated" component is no longer checked.
  • Assuming a default applies to null. Parameter defaults replace only undefined.
  • Using defaultProps with an object or array and mutating it, which shares one object across all renders. Build a fresh value inside the component (or use a constant that you never mutate).
  • Validating what TypeScript already guarantees, which adds code that can drift from the types.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What happens to Component.propTypes on a function component in React 19?

    React 19 removed the propTypes check. The property is silently ignored, so use TypeScript, JSDoc or runtime schemas instead.
  2. 2. Which is the modern way to give a function component's prop a default?

    Default parameter values are plain JavaScript, work in every React version and keep the default beside the code that uses it.
  3. 3. A component has { size = 'md' } and receives size={null}. What is size?

    JavaScript default values apply only when the value is undefined. null is a deliberate value and is kept.
  4. 4. Which validator would you use to require one of the strings 'sm', 'md' or 'lg'?

    oneOf accepts a fixed list of values. In TypeScript the equivalent is the union type 'sm' | 'md' | 'lg'.
  5. 5. Why can TypeScript not replace all runtime validation?

    TypeScript checks your code, not the actual data that arrives while the app runs. Validate untrusted input with a schema where it enters the app.

Summary

  • PropTypes (the prop-types package) warned in development when a prop had the wrong type; defaultProps filled in missing props.
  • React 19 ignores propTypes and no longer supports defaultProps on function components; class components keep defaultProps.
  • Use default parameter values for defaults, TypeScript or JSDoc for static checks, and a schema library for data from outside.
  • Defaults apply to undefined only, never to null.
  • Migrate by moving defaults into parameters and replacing propTypes with types.

Next: Children and Composition