0
0
Reactframework~20 mins

Reusable UI components in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reusable UI 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 accepts a label and onClick function as props. What will be displayed and how will it behave when clicked?
React
import React from 'react';

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

export default function App() {
  const handleClick = () => alert('Clicked!');
  return <Button label="Press me" onClick={handleClick} />;
}
AA button with text 'Click me' that shows an alert 'Clicked!' when clicked.
BA button with no text that does nothing when clicked.
CA button with text 'Press me' that shows an alert 'Clicked!' when clicked.
DA button with text 'Press me' but clicking it causes an error.
Attempts:
2 left
💡 Hint
Look at the props passed to the Button component and what the onClick function does.
state_output
intermediate
2:00remaining
What is the displayed count after clicking the button twice?
This reusable counter component uses React state. What number will be shown after clicking the button two times?
React
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </>
  );
}

export default Counter;
ACount: 0
BCount: 2
CCount: NaN
DCount: 1
Attempts:
2 left
💡 Hint
Each click increases count by 1 starting from 0.
📝 Syntax
advanced
2:30remaining
Which option correctly defines a reusable input component with controlled value?
Choose the code that correctly implements a controlled input component in React that updates its value on user typing.
A
function TextInput({ value, onChange }) {
  return &lt;input value={value} onChange={e =&gt; onChange(e.target.value)} /&gt;;
}
B
function TextInput({ value, onChange }) {
  return &lt;input defaultValue={value} onChange={e =&gt; onChange(e.target.value)} /&gt;;
}
C
function TextInput({ value, onChange }) {
  return &lt;input value={value} onChange={onChange} /&gt;;
}
D
function TextInput({ value, onChange }) {
  return &lt;input onChange={e =&gt; onChange(e.target.value)} /&gt;;
}
Attempts:
2 left
💡 Hint
Controlled inputs use the value prop and update via onChange with event target value.
🔧 Debug
advanced
2:30remaining
Why does this reusable list component not update when items change?
This component receives an array of items as props and renders them. However, when the items array changes, the list does not update. What is the likely cause?
React
function ItemList({ items }) {
  const [list, setList] = React.useState(items);
  return <ul>{list.map(item => <li key={item.id}>{item.name}</li>)}</ul>;
}
AThe component copies items to state only once and never updates state when props change.
BThe key prop is missing, so React cannot track list items properly.
CThe items prop is mutated directly causing React to ignore changes.
DThe map function is used incorrectly causing a runtime error.
Attempts:
2 left
💡 Hint
State initialized from props does not update automatically when props change.
🧠 Conceptual
expert
1:30remaining
Which statement best describes the benefit of reusable UI components?
Why do developers create reusable UI components in frameworks like React?
ATo avoid using props and state for managing UI behavior.
BTo make the app slower by adding more layers of abstraction.
CTo force developers to write more code for each UI element.
DTo write code once and use it many times, improving consistency and reducing bugs.
Attempts:
2 left
💡 Hint
Think about code reuse and maintenance benefits.