0
0
Reactframework~20 mins

Reusable components in React - Practice Problems & Coding Challenges

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