0
0
Reactframework~20 mins

Default props in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Default Props Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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 />;
}
A<p>Hello, !</p>
B<p>Hello, undefined!</p>
C<p>Hello, Guest!</p>
DComponent throws an error
Attempts:
2 left
💡 Hint
Look at the default value assigned in the function parameter.
📝 Syntax
intermediate
2: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>;
}
AButton.defaultProps = { color: 'blue' };
BButton.defaultProps.color = 'blue';
CButton.defaultProps = () => ({ color: 'blue' });
DButton.defaultProps = { color };
Attempts:
2 left
💡 Hint
Remember how to assign an object to defaultProps.
state_output
advanced
2: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 />;
}
AComponent throws an error
BCount: 0
CCount: undefined
DCount: 5
Attempts:
2 left
💡 Hint
Check the default value used in useState initialization.
🔧 Debug
advanced
2: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} />;
}
AThe defaultProps object is missing a key, so it fails silently.
BPassing name={undefined} overrides defaultProps, so name is undefined.
CdefaultProps only works for class components, not functional ones.
DReact does not support defaultProps on any component.
Attempts:
2 left
💡 Hint
Think about how React treats explicit undefined props.
🧠 Conceptual
expert
3: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.
ADefault props can be set by assigning an object to the component's defaultProps property, but default parameter values in the function signature are preferred in modern React.
BDefault props only work if you use class components; functional components ignore defaultProps.
CYou must always use the useDefaultProps hook to set default props in functional components.
DDefault props are automatically inferred from prop types and do not require explicit setting.
Attempts:
2 left
💡 Hint
Consider modern React best practices for default values.