Skip to content

How to Fix React Component defaultProps Not Working

DodaTech Updated 2026-06-26 1 min read

The Problem

In this quick-fix, you'll learn how to set default props for functional and class components. This matters because missing defaults cause undefined errors in production. In real-world applications, this pattern appears in UI component libraries with thousands of prop consumers.

The Wrong Way

function Button({ color, size, label }) {
  return <button style={{ color }}>{label}</button>;
}
<Button label="Click" />
color -> undefined (no default applied)

The Right Way

function Button({ color = "blue", size = "16px", label }) {
  return <button style={{ color }}>{label}</button>;
}
Button.defaultProps = { color: "blue", size: "16px" };
<Button label="Click" />
Renders blue button with defaults applied correctly

Prevention

  • Define defaultProps for every optional prop
  • Use default parameters in destructuring
  • Mark required vs optional props with PropTypes
  • Test components with missing props

FAQ

### Why does React use defaultProps vs default parameters??

defaultProps is React-specific for when prop is undefined. Default params are JS language feature. defaultProps works with PropTypes validation while default params do not. Both work in React 18.

Why are my defaultProps not being applied??

This happens when the prop is passed as null instead of undefined. React only applies defaultProps for undefined. Also check if a HOC is not forwarding props correctly.

Should I use defaultProps or default parameters for modern React??

For functional components, both work. Default parameters are more idiomatic in modern JS. defaultProps integrates with PropTypes. If using TypeScript, default function params are the standard.

This guide is brought to you by DodaTech — built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro