Default Props
Default props give a component a fallback value. A fallback value is used when no prop is passed.
Set a fallback
Friend is used when no name arrives.
const name = props.name || "Friend"No prop passed
The fallback renders in place of the prop.
<Welcome />A prop beats the fallback
Ada replaces Friend in the heading.
<Welcome name="Ada" />Example
function Welcome(props) {
const name = props.name || "Friend"
return <h1>Hello, {name}!</h1>
}
export default function App() {
return (
<>
<Welcome />
<Welcome name="Ada" />
</>
)
}
Key points
- A missing prop has no value.
- The || operator picks the right side when the left side is empty.
- A passed prop beats the fallback value.
- Each prop can have its own fallback.
In this example, Welcome takes props as its first parameter. Welcome stores props.name or Friend in a const. The first use passes no name. The second use passes Ada. The page shows Friend first and Ada second.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What renders when Welcome is used with no name prop?