Challenge - 5 Problems
Default Props Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will this React component render?
Consider this React functional component using default props. What will be the output when rendered without any props?
React
import React from 'react'; function Greeting({ name = 'Guest' }) { return <p>Hello, {name}!</p>; } export default function App() { return <Greeting />; }
Attempts:
2 left
💡 Hint
Look at the default value assigned in the function parameter.
✗ Incorrect
The component Greeting uses a default value 'Guest' for the prop 'name'. When no prop is passed, it uses 'Guest' and renders 'Hello, Guest!'.
📝 Syntax
intermediate2:00remaining
Which option correctly sets default props for a functional component?
You want to set default props for a React functional component named Button. Which code snippet correctly sets the default prop 'color' to 'blue'?
React
function Button({ color }) { return <button style={{ color }}>{color}</button>; }
Attempts:
2 left
💡 Hint
Remember how to assign an object to defaultProps.
✗ Incorrect
The correct way to set default props on a functional component is to assign an object to the static property defaultProps.
❓ state_output
advanced2:00remaining
What is the rendered output of this component?
Given the component below, what will be the text content rendered when is used without props?
React
import React, { useState } from 'react'; function Counter({ start = 5 }) { const [count, setCount] = useState(start); return <p>Count: {count}</p>; } export default function App() { return <Counter />; }
Attempts:
2 left
💡 Hint
Check the default value used in useState initialization.
✗ Incorrect
The default prop 'start' is 5, so useState initializes count to 5, rendering 'Count: 5'.
🔧 Debug
advanced2:00remaining
Why does this component render 'Hello, undefined!'?
Examine the code below. Why does the component render 'Hello, undefined!' instead of using the default prop?
React
function Welcome({ name }) { return <h1>Hello, {name}!</h1>; } Welcome.defaultProps = { name: 'Friend' }; export default function App() { return <Welcome name={undefined} />; }
Attempts:
2 left
💡 Hint
Think about how React treats explicit undefined props.
✗ Incorrect
When a prop is explicitly passed as undefined, React uses that value instead of defaultProps. So name is undefined and renders as such.
🧠 Conceptual
expert3:00remaining
Which statement about default props in React functional components is true?
Select the correct statement about how default props work in React functional components.
Attempts:
2 left
💡 Hint
Consider modern React best practices for default values.
✗ Incorrect
While defaultProps still works on functional components, using default parameter values in the function signature is the modern and preferred approach.