Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

defaultProps stopped working on a function component in React 19

ErrorA prop is undefined after upgrading to React 19 although Component.defaultProps sets it

Short answer

React 19 removed defaultProps for function components, so the defaults are never applied and props arrive as undefined. Replace them with default values in the parameter destructuring. propTypes checks are also removed and silently ignored. Class components still support defaultProps.

  • Applies to React 18.3, React 19, TypeScript 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List every function component that still sets defaultProps or propTypes. React 18.3 is identical to 18.2 but adds warnings for APIs removed in 19, so run it once to see them all.

    grep -rn 'defaultProps\|propTypes' src/
  2. 2Match what you see, then jump to its fix

    1. 1Heading.defaultProps = {...} sits below a function component, and the default text is missingdefaultProps on a function componentFix
    2. 2Wrong prop types no longer print warnings after the upgradepropTypes on a function componentFix

What does the error look like?

  • A prop is undefined after upgrading to React 19 although Component.defaultProps sets it
  • propTypes warnings no longer appear

Cause 1 of 2

defaultProps on a function component

1Check

Check that the component is a function, not a class. Only function components lost the feature.

grep -rn -B3 'defaultProps' src/ | grep -E 'function|=>|class'

2Fix

Move each default into the destructured parameters, and type the prop as optional.

interface Props {
  text?: string;
}

function Heading({ text = 'Hello, world!' }: Props) {
  return <h1>{text}</h1>;
}

3Verify

Render the component without the prop. The default text appears.

npm test

Confirmed by react.dev

Did this fix it?

Cause 2 of 2

propTypes on a function component

1Check

In React 19 the propTypes check is removed from the React package and using it is silently ignored.

grep -rn 'prop-types' package.json src/

2Fix

Move to TypeScript or another type checker. The upgrade guide offers a codemod for the conversion.

npx codemod@latest react/prop-types-typescript

3Verify

Pass a wrong prop type. The type checker now reports it.

npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Upgrade to React 18.3 first and clear its warnings before moving to 19.
  • Use default parameters for every new component, never defaultProps.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.