Bird
Raised Fist0
Reactframework~20 mins

Reusable components in React - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Reusable Components Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this reusable button component?

Consider this React component that reuses a button with different labels and click handlers.

function MyButton({ label, onClick }) {
  return <button onClick={onClick}>{label}</button>
}

function App() {
  const handleClick = () => alert('Clicked!')
  return <MyButton label="Press me" onClick={handleClick} />
}

What happens when the button is clicked?

React
function MyButton({ label, onClick }) {
  return <button onClick={onClick}>{label}</button>
}

function App() {
  const handleClick = () => alert('Clicked!')
  return <MyButton label="Press me" onClick={handleClick} />
}
ANothing happens because onClick is not passed correctly
BAn alert box shows the message 'Clicked!'
CThe button label changes to 'Clicked!'
DA console error occurs due to missing event parameter
Attempts:
2 left
💡 Hint

Check how the onClick prop is used and what the handler does.

❓ state_output
intermediate
2:00remaining
What is the displayed count after clicking the reusable counter button twice?

Look at this reusable counter button component:

function CounterButton() {
  const [count, setCount] = React.useState(0)
  return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
}

function App() {
  return <CounterButton />
}

If the user clicks the button two times, what text is shown on the button?

React
function CounterButton() {
  const [count, setCount] = React.useState(0)
  return <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
}

function App() {
  return <CounterButton />
}
AClicked 0 times
BClicked 1 times
CClicked NaN times
DClicked 2 times
Attempts:
2 left
💡 Hint

Think about how state updates after each click.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a reusable React component that accepts children?

Which of the following code snippets correctly defines a reusable React component that renders its children inside a div?

Afunction Container({ children }) { return <div>{children}</div> }
Bfunction Container(children) { return <div>{children}</div> }
Cfunction Container(props) { return <div>{props.children}</div> }
Dfunction Container() { return <div>{children}</div> }
Attempts:
2 left
💡 Hint

Remember how to access children passed between component tags.

🔧 Debug
advanced
2:00remaining
Why does this reusable component cause a runtime error?

Examine this React component:

function List({ items }) {
  return (
    <ul>
      {items.map(item => <li key={item.id}>{item.name}</li>)}
    </ul>
  )
}

function App() {
  const data = [{ id: 1, name: 'Apple' }, { id: 2 }]
  return <List items={data} />
}

What error will occur when rendering App?

React
function List({ items }) {
  return (
    <ul>
      {items.map(item => <li key={item.id}>{item.name}</li>)}
    </ul>
  )
}

function App() {
  const data = [{ id: 1, name: 'Apple' }, { id: 2 }]
  return <List items={data} />
}
ANo error, renders list with 'Apple' and empty item
BKey warning: Each child in a list should have a unique 'key' prop
CTypeError: Cannot read property 'name' of undefined
DReferenceError: item is not defined
Attempts:
2 left
💡 Hint

Check the data passed and what properties are accessed inside the map.

🧠 Conceptual
expert
2:00remaining
Which option best explains why reusable components improve React app maintainability?

Why do reusable components help maintain a React application better?

AThey force developers to write more code but improve styling flexibility
BThey increase the app size but improve performance by caching components
CThey reduce code duplication and centralize logic, making updates easier and consistent
DThey prevent any bugs by isolating all state in one place
Attempts:
2 left
💡 Hint

Think about how reusing code affects changes and bugs.

Practice

(1/5)
1. What is the main benefit of using reusable components in React?
easy
A. They make your app run faster by default
B. They let you write less code and keep your app organized
C. They automatically fix bugs in your code
D. They prevent users from clicking buttons

Solution

  1. Step 1: Understand the purpose of reusable components

    Reusable components are designed to be used multiple times to avoid repeating code.
  2. Step 2: Identify the main benefit

    Using reusable components helps keep the app organized and reduces the amount of code you write.
  3. Final Answer:

    They let you write less code and keep your app organized -> Option B
  4. Quick Check:

    Reusable components = less code + organized app [OK]
Hint: Reusable means use again to save code and stay organized [OK]
Common Mistakes:
  • Thinking reusable components speed up the app automatically
  • Believing they fix bugs without coding
  • Confusing reusable components with user interaction controls
2. Which of the following is the correct way to pass a prop named title with value "Hello" to a reusable component Card?
easy
A. <Card title=Hello />
B. <Card title={Hello} />
C. <Card "Hello"=title />
D. <Card title="Hello" />

Solution

  1. Step 1: Recall JSX prop syntax

    Props are passed as attributes with name and value inside quotes or curly braces for expressions.
  2. Step 2: Check each option

    <Card title="Hello" /> uses correct syntax: title="Hello". <Card title={Hello} /> misses quotes around string. <Card title=Hello /> lacks quotes or braces. <Card "Hello"=title /> has incorrect attribute format.
  3. Final Answer:

    <Card title="Hello" /> -> Option D
  4. Quick Check:

    Props need name="value" syntax [OK]
Hint: Props look like HTML attributes with quotes [OK]
Common Mistakes:
  • Forgetting quotes around string prop values
  • Using curly braces without variables
  • Swapping prop name and value order
3. What will be the output of this React component usage?
function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

<Greeting name="Alice" />
<Greeting name="Bob" />
medium
A.

Hello, Alice!

Hello, Bob!

B.

Hello, Alice!

C.

Hello, Bob!

D. Error: Cannot render two components like this

Solution

  1. Step 1: Understand component usage

    Each <Greeting /> renders an <h1> with the passed name prop.
  2. Step 2: Check the two usages

    First renders Hello, Alice!, second renders Hello, Bob!. Both outputs appear in order.
  3. Final Answer:

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

    Two components with different props render two greetings [OK]
Hint: Each component instance renders separately with its props [OK]
Common Mistakes:
  • Thinking only one component renders
  • Believing React errors on multiple components
  • Confusing props with state causing wrong output
4. Identify the error in this reusable component code:
function Button(props) {
  return <button>Click {props.label}</button>
}

// Usage
<Button label=Submit />
medium
A. Button component must return a div, not button
B. Props cannot be used inside JSX
C. Missing quotes around the prop value in usage
D. Component name must start with lowercase

Solution

  1. Step 1: Check prop usage syntax

    In JSX, string prop values must be in quotes or curly braces.
  2. Step 2: Identify the error

    The usage <Button label=Submit /> lacks quotes around "Submit" causing syntax error.
  3. Final Answer:

    Missing quotes around the prop value in usage -> Option C
  4. Quick Check:

    String props need quotes [OK]
Hint: String props always need quotes in JSX [OK]
Common Mistakes:
  • Thinking button must be wrapped in div
  • Believing props can't be used inside JSX
  • Using lowercase component names
5. You want to create a reusable List component that shows items with a custom renderItem prop function. Which approach correctly uses renderItem inside the component?
hard
A.
function List(props) {
  return <ul>{
    props.items.map(item => <li>{props.renderItem(item)}</li>)
  }</ul>;
}
B.
function List(props) {
  return <ul>{
    props.items.map(props.renderItem(item))
  }</ul>;
}
C.
function List(props) {
  return <ul>{
    props.renderItem(props.items)
  }</ul>;
}
D.
function List(props) {
  return <ul>{
    props.items.forEach(item => props.renderItem(item))
  }</ul>;
}

Solution

  1. Step 1: Understand how to use renderItem with map

    To render a list, use map to transform each item into JSX, calling renderItem for each.
  2. Step 2: Check each option

    function List(props) {
      return <ul>{
        props.items.map(item => <li>{props.renderItem(item)}</li>)
      }</ul>;
    }
    correctly maps items and calls renderItem inside <li>.
    function List(props) {
      return <ul>{
        props.items.map(props.renderItem(item))
      }</ul>;
    }
    calls renderItem incorrectly inside map.
    function List(props) {
      return <ul>{
        props.renderItem(props.items)
      }</ul>;
    }
    calls renderItem once with whole array.
    function List(props) {
      return <ul>{
        props.items.forEach(item => props.renderItem(item))
      }</ul>;
    }
    uses forEach which returns undefined, so no output.
  3. Final Answer:

    Option A uses map and renderItem correctly inside list -> Option A
  4. Quick Check:

    Use map with renderItem for lists [OK]
Hint: Use map with renderItem inside JSX to render lists [OK]
Common Mistakes:
  • Using forEach instead of map for rendering
  • Calling renderItem outside map or with wrong argument
  • Passing whole array to renderItem instead of single item
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