Default Props allow you to set fallback values for props in a React component. If a parent component does not pass a specific prop, React automatically uses the default value defined in defaultProps or via parameter defaults.
This keeps components stable, predictable, and easy to reuse, even when some data is missing.
⚛️ React · ? Props · ? Fallback Values
undefined or broken UI.Component.defaultProps or parameter defaults.Traditionally, default props are defined using the defaultProps property on the component:
For functional or class components:
MyComponent.defaultProps = { propName: defaultValue };In modern React, especially with functional components, it is very common to use parameter defaults instead:
function MyComponent({ propName = "Default" }) { ... }Here is a simple component that greets the user. If no name is provided, it falls back to "Guest".
// Welcome.js - component with default props
function Welcome({ name }) {
return <h2>Welcome, {name}!</h2>;
}
Welcome.defaultProps = {
name: "Guest"
};
export default Welcome;
// App.js - using Welcome with and without props
import Welcome from "./Welcome";
function App() {
return (
<div>
<Welcome />
<Welcome name="Riya" />
</div>
);
}
export default App;
When <Welcome /> is rendered without a name prop:
✅ Welcome, Guest!
When <Welcome name="Riya" /> is rendered with a name prop:
✅ Welcome, Riya!
In the first case, React uses the default value "Guest" because the name prop was not provided by the parent.
undefined values when props are missing.You can define default values for multiple props in the same component. If any of them are not provided by the parent, React falls back to these defaults.
// Product.js - multiple default props for a product card
function Product({ name, price, inStock }) {
return (
<div>
<h3>{name}</h3>
<p>Price: ₹{price}</p>
<p>Availability: {inStock ? "In Stock" : "Out of Stock"}</p>
</div>
);
}
Product.defaultProps = {
name: "Unknown Product",
price: 0,
inStock: false
};
export default Product;
If the parent renders <Product /> with no props:
name → "Unknown Product"price → 0inStock → false (shows "Out of Stock")Default props work the same way with class components. You still attach a defaultProps object to the component.
// Greeting.js - class component with default props
import React, { Component } from "react";
class Greeting extends Component {
render() {
return <h2>Hello, {this.props.name}!</h2>;
}
}
Greeting.defaultProps = {
name: "Visitor"
};
export default Greeting;
If no name is passed to Greeting, it will show: "Hello, Visitor!"
Instead of using defaultProps, you can assign default values directly inside the function parameters. This is a modern and concise approach.
// Welcome.js - using parameter default instead of defaultProps
function Welcome({ name = "Guest" }) {
return <h2>Welcome, {name}!</h2>;
}
export default Welcome;
Here, if name is not provided, it automatically takes the value "Guest" from the function parameter default.
The defaultProps property still works, but for functional components defined with ES6 syntax (especially when using destructuring), using parameter defaults is now the preferred and more idiomatic approach.
For class components or older codebases, defaultProps is still very common and valid.
defaultProps or parameter defaults for props that are truly optional.PropTypes (or TypeScript) for safer, self-documenting components.Message component with props sender and text.sender = "Admin" and text = "Welcome to the platform!".sender="Support" and a custom text.defaultProps.Goal: Learn how to provide fallback values to components using defaultProps and parameter defaults to make them reliable and reusable.