What if your components could never break from missing information?
Why Default props in React? - Purpose & Use Cases
Imagine you build a React component that shows a user profile. Sometimes you forget to pass the user's name, so the component shows nothing or breaks.
Without default props, you must check every prop manually to avoid errors or empty spaces. This makes your code messy and easy to forget, causing bugs and bad user experience.
Default props let you set fallback values for your component's inputs. If a prop is missing, React uses the default automatically, keeping your UI consistent and your code clean.
function Profile({ name }) {
return <p>{name ? name : 'Guest'}</p>;
}function Profile({ name = 'Guest' }) {
return <p>{name}</p>;
}It enables components to work smoothly even when some data is missing, improving reliability and developer happiness.
Think of a greeting card app that says "Hello, Guest" if no name is given, instead of showing a blank or error.
Default props prevent UI breaks from missing data.
They keep your code simpler and easier to read.
They improve user experience by providing sensible fallbacks.
Practice
Solution
Step 1: Understand what props are
Props are inputs to React components that can be passed from parent components.Step 2: Identify the role of default props
Default props provide fallback values when no value is passed for a prop, preventing undefined values.Final Answer:
To provide fallback values for props that are not passed -> Option DQuick Check:
Default props = fallback values [OK]
- Confusing default props with state
- Thinking default props enforce types
- Assuming default props improve performance
Solution
Step 1: Review ES6 default parameter syntax
Default values can be set directly in the function parameter list using destructuring with defaults.Step 2: Check each option
function MyComponent({ name = 'Guest' }) { return <div>{name}</div>; } correctly uses destructuring with a default value: { name = 'Guest' }.Final Answer:
function MyComponent({ name = 'Guest' }) { return <div>{name}</div>; } -> Option BQuick Check:
Default props in parameters = function MyComponent({ name = 'Guest' }) { return <div>{name}</div>; } [OK]
- Trying to set default props inside the function body
- Using defaultProps with functional components incorrectly
- Assigning default props to the whole props object
function Greeting({ name = 'Friend' }) {
return <h1>Hello, {name}!Solution
Step 1: Check the default prop value
The component uses destructuring with a default value: name = 'Friend'.Step 2: Analyze the usage without passing name
No prop is passed, so the default 'Friend' is used.Final Answer:
<h1>Hello, Friend!</h1> -> Option AQuick Check:
Default prop used = 'Friend' [OK]
- Assuming missing props cause errors
- Expecting undefined instead of default
- Confusing default props with required props
function Button(props) {
const { label } = props;
return <button>{label}</button>;
}
Button.defaultProps = { label: 'Click me' };
// Usage:
<Button />Solution
Step 1: Understand defaultProps on functional components
React supports defaultProps on functional components, so this syntax is valid.Step 2: Check usage and output
Since no label prop is passed, defaultProps provides 'Click me', so the button shows that text.Final Answer:
No error; this code works correctly -> Option CQuick Check:
defaultProps works on functions = No error; this code works correctly [OK]
- Thinking defaultProps is deprecated for functional components
- Placing defaultProps inside the function body
- Confusing defaultProps with state initialization
function Profile({ user = { name: 'Guest', age: 0 } }) {
return <div>Name: {user.name}, Age: {user.age}</div>;
}Solution
Step 1: Understand default props for nested objects
Setting default nested objects in parameters prevents errors when accessing properties.Step 2: Compare approaches
Use destructuring with a default object in the parameter as shown uses destructuring with a default object, which safely provides nested defaults.Final Answer:
Use destructuring with a default object in the parameter as shown -> Option AQuick Check:
Default nested object in parameters = Use destructuring with a default object in the parameter as shown [OK]
- Setting defaultProps with nested objects can cause shallow merge issues
- Initializing defaults inside function body can be verbose and error-prone
- Not providing defaults leads to runtime errors accessing nested props
Ready to prove it?
Pass the test and this pattern moves unknown
