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
This guide is brought to you by DodaTech — built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
← Previous
How to Fix React Children Prop Type Errors
Next →
How to Fix React Component displayName Missing
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro