Default Props Values
Learn how to provide fallback values to React components when optional props are not supplied by the parent component.
01 What Are Default Props Values?
Default props values are fallback values used when a component does not receive a particular prop value. They help prevent optional information from appearing empty in the UI.
Samadhan
Guest
The first value can be supplied; the second can use a default.
02 Default Values Using Destructuring
A simple way to define defaults in a functional component is to assign fallback values while destructuring the props.
function Student({ name = "Guest" }) {
return <h2>Student Name: {name}</h2>;
}
export default Student;
When the parent renders <Student />, the default name is used.
Student Name: Guest
03 Passing a Custom Value
When the parent supplies a value, React uses that value instead of the fallback value.
function App() {
return <Student name="Samiksha" />;
}
function Student({ name = "Guest" }) {
return <h2>Student Name: {name}</h2>;
}
export default App;
Student Name: Samiksha
04 Multiple Default Props
You can assign default values to multiple props in the same component, such as name, course and city.
function Student({
name = "Guest",
course = "Not Assigned",
city = "Pune"
}) {
return (
<div>
<h2>{name}</h2>
<p>Course: {course}</p>
<p>City: {city}</p>
</div>
);
}
export default Student;
Guest
Course: Not Assigned
City: Pune
05 Mixing Provided and Default Values
A parent can provide some props and leave others unspecified. Only the missing or undefined props use their defaults.
function App() {
return (
<Student
name="Pradnya"
course="Python"
/>
);
}
function Student({
name = "Guest",
course = "Not Assigned",
city = "Pune"
}) {
return (
<div>
<h2>{name}</h2>
<p>Course: {course}</p>
<p>City: {city}</p>
</div>
);
}
export default App;
Pradnya
Course: Python
City: Pune
06 Default Values for Numbers
Defaults also work with numbers. Use a numeric value directly rather than writing it as a string.
function Student({ name = "Guest", age = 18 }) {
return (
<div>
<h2>{name}</h2>
<p>Age: {age}</p>
</div>
);
}
export default Student;
Guest
Age: 18
07 Default Values for Arrays
An array can also have a default value. Here, an empty array is used when the parent does not provide a skills prop.
function Student({
name = "Guest",
skills = []
}) {
return (
<div>
<h2>{name}</h2>
<h3>Skills
Guest
Skills
No skills added yet.
08 Default Values with Reusable Components
Default props are helpful in reusable components because the component can still display meaningful content when optional information is not supplied.
function App() {
return (
<div>
<Course name="React JS" duration="3 Months" />
<Course name=".NET" />
<Course />
</div>
);
}
function Course({
name = "General Course",
duration = "Duration not specified"
}) {
return (
<div>
<h3>{name}</h3>
<p>Duration: {duration}</p>
</div>
);
}
export default App;
React JS
Duration: 3 Months
.NET
Duration: Duration not specified
General Course
Duration: Duration not specified
09 Default Value vs Null and Empty String
A destructuring default is used when a prop is undefined. It does not replace null or an empty string automatically.
function App() {
return (
<div>
<Student />
<Student name={null} />
<Student name="" />
</div>
);
}
function Student({ name = "Guest" }) {
return (
<p>
Name: {name === null
? "null"
: name === ""
? "empty string"
: name}
</p>
);
}
export default App;
Name: Guest
Name: null
Name: empty string
10 Important Rules for Default Props
- Defaults provide fallback values for omitted or undefined props.
- A value explicitly provided by the parent takes precedence.
- Defaults can be declared during parameter destructuring.
- Defaults can be used for strings, numbers, arrays and objects.
- Passing null does not trigger a destructuring default.
- Default values do not make props mutable.
This section explains the rules for default values. It does not render a separate React component.
Summary
Default props values provide fallback content when optional props are omitted or undefined. In functional components, defaults can be assigned directly during parameter destructuring. The parent component's supplied values take precedence over defaults, while null and empty strings remain distinct values. Default values make reusable components more reliable, readable and easier to maintain in React applications at CIIT Training Institute.