0
0
Reactframework~20 mins

Controlled components in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Controlled 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 controlled input component?

Consider this React component that uses a controlled input. What will be displayed when the user types 'hello'?

React
import React, { useState } from 'react';

function ControlledInput() {
  const [text, setText] = useState('');

  return (
    <div>
      <input
        type="text"
        value={text}
        onChange={e => setText(e.target.value)}
        aria-label="input-text"
      />
      <p>{text}</p>
    </div>
  );
}

export default ControlledInput;
AThe input will not accept any typing.
BThe paragraph will display 'hello' as the user types.
CThe paragraph will remain empty regardless of typing.
DThe paragraph will display the reversed string of the input.
Attempts:
2 left
💡 Hint

Think about how the state updates on input change and how it affects the paragraph.

state_output
intermediate
2:00remaining
What is the value of state after this input change?

Given this controlled component, what will be the value of name state after the user types 'React'?

React
import React, { useState } from 'react';

function NameInput() {
  const [name, setName] = useState('');

  function handleChange(e) {
    setName(e.target.value.toUpperCase());
  }

  return <input type="text" value={name} onChange={handleChange} aria-label="name-input" />;
}

export default NameInput;
A"React"
B"react"
C"REACT"
D"" (empty string)
Attempts:
2 left
💡 Hint

Look at how the input value is transformed before setting state.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this controlled component?

Identify which code snippet will cause a syntax error when used as a controlled input in React.

Aconst [value, setValue] = useState(''); return <input value={value} onChange={e => setValue(e.target.value) />;
Bconst [value, setValue] = useState(''); return <input value={value} onChange={e => setValue(e.target.value)} />;
Cconst [value, setValue] = useState(''); return <input value={value} onChange={e => setValue(e.target.value) } />;
D;>/ })eulav.tegrat.e(eulaVtes >= e{=egnahCno }eulav{=eulav tupni< nruter ;)''(etatSesu = ]eulaVtes ,eulav[ tsnoc
Attempts:
2 left
💡 Hint

Check the JSX syntax carefully, especially the closing of the input tag.

🔧 Debug
advanced
2:00remaining
Why does this controlled input not update on typing?

Look at this React component. Why does the input not show any typed characters?

React
import React, { useState } from 'react';

function BrokenInput() {
  const [text, setText] = useState('');

  return <input type="text" value={text} onChange={() => setText('')} aria-label="broken-input" />;
}

export default BrokenInput;
AThe value prop is missing, so input is uncontrolled and does not update.
BThe input type is incorrect and does not accept text.
CThe useState hook is not initialized properly.
DThe onChange handler resets the state to empty string on every change, so input never updates.
Attempts:
2 left
💡 Hint

Check what the onChange function does to the state.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of controlled components in React?

Why do React developers prefer controlled components for form inputs?

AThey allow React state to fully control the form data, enabling validation and conditional rendering easily.
BThey automatically optimize performance by avoiding re-renders.
CThey require less code than uncontrolled components.
DThey allow direct DOM manipulation for faster updates.
Attempts:
2 left
💡 Hint

Think about how controlled components relate to React state and UI updates.