Recently, while building a React dashboard for a client in New York, I needed to create a component that could accept some props, but not all of them were required.
That’s when I realized many developers struggle with optional props in React. We often want flexibility, a component that works even if certain props aren’t passed.
So, in this tutorial, I’ll show you how I handle optional props in React. I’ll cover both JavaScript and TypeScript examples, share a few real-world use cases, and explain how to make your components more reusable and maintainable.
What Are Optional Props in React?
In React, props (short for “properties”) are inputs you pass to components. They help make your components dynamic and reusable.
However, not all props are required. Sometimes you want a prop to be optional, meaning, if it’s not provided, the component still works fine (maybe with a default behavior or value).
For example, imagine a <UserCard /> component that shows a user’s name and location. The location might not always be available, so that prop should be optional.
Method 1 – Use Default Props in Functional Components
The simplest way to make props optional in React (using JavaScript) is by assigning default values directly in the function parameters.
Here’s how I usually do it:
import React from "react";
function UserCard({ name, location = "Location not available" }) {
return (
<div style={styles.card}>
<h2>{name}</h2>
<p>{location}</p>
</div>
);
}
const styles = {
card: {
border: "1px solid #ccc",
padding: "10px 15px",
borderRadius: "8px",
width: "250px",
margin: "10px auto",
textAlign: "center",
fontFamily: "Arial, sans-serif",
},
};
// Example usage
export default function App() {
return (
<div>
<UserCard name="John Doe" location="Texas, USA" />
<UserCard name="Jane Smith" />
</div>
);
}You can see the output in the screenshot below.

- The location prop is optional.
- If you don’t pass it, React automatically uses “Location not available”.
- This makes the component flexible and easy to reuse across different parts of your app.
Method 2 – Use DefaultProps (Older Syntax)
Before React 17, many developers used the defaultProps property to define optional props. Although it’s less common now (since function parameter defaults are cleaner), you might still see it in older codebases.
Here’s how it looks:
import React from "react";
function Greeting({ name, message }) {
return (
<h3>
{message}, {name}!
</h3>
);
}
Greeting.defaultProps = {
message: "Hello",
};
// Example usage
export default function App() {
return (
<div>
<Greeting name="Michael" />
<Greeting name="Sarah" message="Welcome" />
</div>
);
}You can see the output in the screenshot below.

- defaultProps still works for class components, but for function components, using default parameters (Method 1) is the modern, recommended approach.
Method 3 – Use Optional Props in TypeScript
If you’re using TypeScript (which I highly recommend for larger React projects), you can explicitly mark props as optional using the ? symbol.
Here’s a real-world example:
import React from "react";
interface ProductCardProps {
title: string;
price?: number; // Optional prop
inStock?: boolean;
}
const ProductCard: React.FC<ProductCardProps> = ({
title,
price = 0,
inStock = true,
}) => {
return (
<div style={styles.card}>
<h2>{title}</h2>
<p>Price: ${price.toFixed(2)}</p>
<p>Status: {inStock ? "In Stock" : "Out of Stock"}</p>
</div>
);
};
const styles = {
card: {
border: "1px solid #ccc",
padding: "10px 15px",
borderRadius: "8px",
width: "250px",
margin: "10px auto",
textAlign: "center",
fontFamily: "Arial, sans-serif",
},
};
// Example usage
export default function App() {
return (
<div>
<ProductCard title="MacBook Air" price={999} inStock={true} />
<ProductCard title="Dell XPS 13" />
</div>
);
}You can see the output in the screenshot below.

- The price and inStock props are optional.
- Even if you don’t pass them, the component still renders correctly.
- TypeScript ensures that if you do pass them, they’re of the correct type (number and boolean).
This is especially useful in enterprise-level React apps where you want type safety and clear documentation for your components.
Method 4 – Conditional Rendering with Optional Props
Sometimes, you don’t want to show anything at all if a prop isn’t provided. In such cases, you can use conditional rendering to check if the prop exists before displaying it.
Here’s an example:
import React from "react";
function EmployeeCard({ name, position, department }) {
return (
<div style={styles.card}>
<h2>{name}</h2>
{position && <p>Position: {position}</p>}
{department && <p>Department: {department}</p>}
</div>
);
}
const styles = {
card: {
border: "1px solid #ccc",
padding: "10px 15px",
borderRadius: "8px",
width: "250px",
margin: "10px auto",
textAlign: "center",
fontFamily: "Arial, sans-serif",
},
};
// Example usage
export default function App() {
return (
<div>
<EmployeeCard name="Emily Johnson" position="Project Manager" />
<EmployeeCard name="Robert Lee" />
</div>
);
}You can see the output in the screenshot below.

- The position and department props are optional.
- If they’re not passed, those lines simply don’t render.
- This approach keeps your UI clean and avoids showing empty placeholders.
Method 5 – Combine Optional Props and Children
Sometimes, you might want to make children optional, too. For example, a <Card> component that optionally displays extra content when passed as children.
import React from "react";
function Card({ title, children }) {
return (
<div style={styles.card}>
<h3>{title}</h3>
{children && <div style={styles.content}>{children}</div>}
</div>
);
}
const styles = {
card: {
border: "1px solid #ccc",
borderRadius: "8px",
padding: "15px",
margin: "10px auto",
width: "300px",
fontFamily: "Arial, sans-serif",
},
content: {
marginTop: "10px",
color: "#555",
},
};
// Example usage
export default function App() {
return (
<div>
<Card title="Employee Overview">
<p>John Doe is a senior developer based in California.</p>
</Card>
<Card title="Empty Card" />
</div>
);
}This pattern is very common in dashboard layouts or modal components, where you may or may not include additional content.
Best Practices for Optional Props
Here are a few tips I follow when working with optional props in React:
- Always define defaults – This prevents undefined errors and improves user experience.
- Use TypeScript for clarity – It makes optional props explicit and self-documenting.
- Avoid too many optional props – If your component has more than 5 optional props, consider splitting it into smaller components.
- Use conditional rendering wisely – Don’t clutter your JSX with too many && checks.
Optional props make your React components flexible, reusable, and easy to maintain.
Whether you’re building a small internal tool or a large-scale SaaS dashboard, knowing how to handle optional props properly will save you time and prevent unnecessary bugs.
I hope you found this tutorial helpful.
You may also like to read:
- Dynamically Render Components in React
- How to Handle React Component Unmount
- Fetch and Display Data from an API in React
- Route Component in React Router

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.