Bird
Raised Fist0
Reactframework~20 mins

Default props in React - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of default props in a React functional component?
easy
A. To enforce type checking on props
B. To optimize component rendering speed
C. To make the component stateful
D. To provide fallback values for props that are not passed

Solution

  1. Step 1: Understand what props are

    Props are inputs to React components that can be passed from parent components.
  2. Step 2: Identify the role of default props

    Default props provide fallback values when no value is passed for a prop, preventing undefined values.
  3. Final Answer:

    To provide fallback values for props that are not passed -> Option D
  4. Quick Check:

    Default props = fallback values [OK]
Hint: Default props fill in missing props automatically [OK]
Common Mistakes:
  • Confusing default props with state
  • Thinking default props enforce types
  • Assuming default props improve performance
2. Which of the following is the correct way to set default props in a React functional component using ES6 syntax?
easy
A. function MyComponent(props = { name: 'Guest' }) { return
{props.name}
; }
B. function MyComponent({ name = 'Guest' }) { return
{name}
; }
C. MyComponent.defaultProps = { name: 'Guest' }; function MyComponent(props) { return
{props.name}
; }
D. function MyComponent(props) { props.name = props.name || 'Guest'; return
{props.name}
; }

Solution

  1. Step 1: Review ES6 default parameter syntax

    Default values can be set directly in the function parameter list using destructuring with defaults.
  2. Step 2: Check each option

    function MyComponent({ name = 'Guest' }) { return <div>{name}</div>; } correctly uses destructuring with a default value: { name = 'Guest' }.
  3. Final Answer:

    function MyComponent({ name = 'Guest' }) { return <div>{name}</div>; } -> Option B
  4. Quick Check:

    Default props in parameters = function MyComponent({ name = 'Guest' }) { return <div>{name}</div>; } [OK]
Hint: Use destructuring with default values in parameters [OK]
Common Mistakes:
  • Trying to set default props inside the function body
  • Using defaultProps with functional components incorrectly
  • Assigning default props to the whole props object
3. What will be rendered by this component?
function Greeting({ name = 'Friend' }) {
  return <h1>Hello, {name}!
medium
A. <h1>Hello, Friend!</h1>
B. <h1>Hello, undefined!</h1>
C. Error: Missing required prop 'name'
D. <h1>Hello, !</h1>

Solution

  1. Step 1: Check the default prop value

    The component uses destructuring with a default value: name = 'Friend'.
  2. Step 2: Analyze the usage without passing name

    No prop is passed, so the default 'Friend' is used.
  3. Final Answer:

    <h1>Hello, Friend!</h1> -> Option A
  4. Quick Check:

    Default prop used = 'Friend' [OK]
Hint: No prop passed? Default value is used [OK]
Common Mistakes:
  • Assuming missing props cause errors
  • Expecting undefined instead of default
  • Confusing default props with required props
4. Identify the error in this component that tries to set default props:
function Button(props) {
  const { label } = props;
  return <button>{label}</button>;
}

Button.defaultProps = { label: 'Click me' };

// Usage:
<Button />
medium
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

  1. Step 1: Understand defaultProps on functional components

    React supports defaultProps on functional components, so this syntax is valid.
  2. Step 2: Check usage and output

    Since no label prop is passed, defaultProps provides 'Click me', so the button shows that text.
  3. Final Answer:

    No error; this code works correctly -> Option C
  4. 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

  1. Step 1: Understand default props for nested objects

    Setting default nested objects in parameters prevents errors when accessing properties.
  2. 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.
  3. Final Answer:

    Use destructuring with a default object in the parameter as shown -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard