← Back to React, page 4

react-0066 · Easy fresher

How do you set default values for props in React 19, and what happened to defaultProps and propTypes?

Use JavaScript default values in the destructured parameter: function Button({ size = 'md' }). React 19 removed defaultProps for function components and removed propTypes checking, so use default parameters and TypeScript instead. Defaults apply when the prop is undefined, not when it is null, which matters when passing null on purpose.

Full answer

A prop that the caller leaves out is undefined. Modern React handles defaults with plain JavaScript: function Button({ size = 'md', children }). The default is visible next to the code that uses it, works with TypeScript and needs no React feature.

In older React you could write Button.defaultProps = { size: 'md' } and Button.propTypes = { size: PropTypes.string }. React 19 stopped supporting both on function components. defaultProps on function components is ignored, so a component that relied on it receives undefined. Class components still support defaultProps. PropTypes checks were removed from the React package, so they no longer run. Use TypeScript for compile-time checks, or a runtime schema library such as Zod for data that comes from outside.

A subtle point: a default parameter applies for undefined only. If a caller passes null, the default is not used, and you need size ?? 'md' inside, or must decide that null is invalid. Another point: a default object or array in the parameter, such as items = [], creates a new value on every render. That can break memoization and effect dependencies, so use a constant defined outside the component when identity matters.

When migrating, search for .defaultProps and convert each to default parameters, then remove PropTypes imports or keep them only for documentation. The codemods in the React 19 upgrade guide help.

Also keep children optional with a default of nothing, and avoid default values that hide real bugs, such as a default user id.

jsx
const NO_ITEMS = [];

function Button({ size = 'md', label, items = NO_ITEMS }) {
  return (
    <button data-size={size} data-items={items.length}>
      {label}
    </button>
  );
}

function Panel({ title = 'Untitled', children }) {
  return (
    <section>
      <h2>{title}</h2>
      {children}
    </section>
  );
}

export default function App() {
  return (
    <Panel>
      <Button label="Default size" />
      <Button label="Large" size="lg" />
      <Button label="Null size" size={null} />
    </Panel>
  );
}

The third button passes null, so the default is not used and data-size is not rendered at all. Rendered result: <section><h2>Untitled</h2><button data-size="md" data-items="0">Default size</button><button data-size="lg" data-items="0">Large</button><button data-items="0">Null size</button></section>

Key points

  • Use default parameters in the destructured props.
  • defaultProps on function components and propTypes checks no longer work in React 19.
  • Defaults apply only for undefined, not null.
  • A default array or object is a new value each render. Use an outer constant if identity matters.
  • Use TypeScript or a schema library for type checking.

Common mistakes

  • Relying on defaultProps in new function components after upgrading to React 19.
  • Expecting a default parameter to replace null.
  • Writing items = [] when the array is used as a dependency.
  • Keeping PropTypes and assuming they still check anything.
  • Using defaults that hide missing required data.

Follow-ups

  • How would you migrate a codebase that uses defaultProps everywhere?
  • How do TypeScript optional props relate to default parameters?
  • How would you treat null as "use the default"?

Tips

  • Mention the null versus undefined rule. It is the detail that shows care.
  • Show you know the React 19 change, not only the old API.
  • Offer TypeScript as the replacement for propTypes.