Default props are values given to a React component's props that are used if no value is provided by the parent component. They help ensure the component works with sensible defaults.
Click to reveal answer
beginner
How do you set default props in a React functional component?
You can set default props by assigning default values in the function parameter list, like <code>function MyComponent({ title = 'Hello' }) { ... }</code>. This way, if <code>title</code> is not passed, it defaults to 'Hello'.
Click to reveal answer
beginner
Why are default props useful in React components?
They prevent errors by providing fallback values, improve component reusability, and make components easier to use without always specifying every prop.
Click to reveal answer
beginner
Can you override default props in React? How?
Yes, you override default props by passing a prop value when using the component. The passed value replaces the default.
Click to reveal answer
beginner
Show an example of a React functional component with default props using ES6 syntax.
Example:<br><pre>function Greeting({ name = 'Friend' }) {<br> return <h1>Hello, {name}!</h1><br>}</pre>
Click to reveal answer
What happens if you don't pass a prop that has a default value in a React component?
AThe component does not render.
BThe component throws an error.
CThe component uses the default value for that prop.
DThe prop value becomes undefined.
✗ Incorrect
If a prop is not passed, React uses the default value defined for that prop.
How do you define default props in a React functional component?
ABy setting default values in the function parameters.
BBy using <code>this.defaultProps</code> inside the function.
A. defaultProps is not supported on functional components
B. The component should use destructuring with default values instead
C. No error; this code works correctly
D. The defaultProps assignment should be inside the function
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 C
Quick Check:
defaultProps works on functions = No error; this code works correctly [OK]
Hint: defaultProps still works on functions but prefer defaults in parameters [OK]
Common Mistakes:
Thinking defaultProps is deprecated for functional components
Placing defaultProps inside the function body
Confusing defaultProps with state initialization
5. You want a component to have a default prop that is an object with nested values. Which approach correctly sets default props to avoid errors if the prop is missing?
function Profile({ user = { name: 'Guest', age: 0 } }) {
return <div>Name: {user.name}, Age: {user.age}</div>;
}
hard
A. Use destructuring with a default object in the parameter as shown
B. Set defaultProps = { user: { name: 'Guest', age: 0 } } after the function
C. Initialize user inside the function body with if (!user) user = {...}
D. Pass user as a required prop and do not set defaults
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 A
Quick Check:
Default nested object in parameters = Use destructuring with a default object in the parameter as shown [OK]
Hint: Set nested defaults in parameter destructuring to avoid undefined errors [OK]
Common Mistakes:
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
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.