Bird
Raised Fist0
Reactframework~20 mins

Handling input fields in React - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Input Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when typing in this controlled input?

Consider this React component that manages an input field:

import React, { useState } from 'react';

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

  return (
    <input
      type="text"
      value={name}
      onChange={e => setName(e.target.value)}
      aria-label="Name input"
    />
  );
}

What will be the value shown in the input box after the user types the characters "abc"?

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

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

  return (
    <input
      type="text"
      value={name}
      onChange={e => setName(e.target.value)}
      aria-label="Name input"
    />
  );
}
AThe input box will show "undefined" because the value is not set properly.
BThe input box will remain empty because the value is controlled and never updated.
CThe input box will show "a" only, ignoring the rest.
DThe input box will show "abc" as typed by the user.
Attempts:
2 left
💡 Hint

Think about how the value and onChange props work together in controlled inputs.

📝 Syntax
intermediate
2:00remaining
Which option correctly updates multiple input fields in React state?

You want to handle two input fields, "email" and "password", using a single state object. Which code snippet correctly updates the state when either input changes?

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

function LoginForm() {
  const [form, setForm] = useState({ email: '', password: '' });

  function handleChange(e) {
    const { name, value } = e.target;
    // Update state here
  }

  return (
    <>
      <input name="email" value={form.email} onChange={handleChange} />
      <input name="password" value={form.password} onChange={handleChange} />
    </>
  );
}
AsetForm(prev => ({ ...prev, [name]: value }));
BsetForm({ [name]: value });
CsetForm({ form, [name]: value });
DsetForm(prev => ({ [name]: value }));
Attempts:
2 left
💡 Hint

Remember to keep the other fields unchanged when updating one field in an object state.

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

Look at this React component:

function TextInput() {
  const [text, setText] = React.useState('');

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

Why does the input box stay empty when typing?

React
function TextInput() {
  const [text, setText] = React.useState('');

  return (
    <input
      type="text"
      value={text}
      onChange={e => setText}
      aria-label="Text input"
    />
  );
}
ABecause setText is not called with the new value; it is passed as a function reference instead of being invoked.
BBecause the initial state is empty and never changes.
CBecause the input is missing a name attribute.
DBecause the onChange event is not supported on input elements.
Attempts:
2 left
💡 Hint

Check how the onChange handler updates the state.

❓ state_output
advanced
2:00remaining
What is the final state after typing 'hi' in this input?

Consider this React component:

function UppercaseInput() {
  const [text, setText] = React.useState('');

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

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

What will be the value of text after the user types the characters "hi"?

React
function UppercaseInput() {
  const [text, setText] = React.useState('');

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

  return (
    <input
      type="text"
      value={text}
      onChange={handleChange}
      aria-label="Uppercase input"
    />
  );
}
A"Hi"
B"hi"
C"HI"
D"hI"
Attempts:
2 left
💡 Hint

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

🧠 Conceptual
expert
2:00remaining
Why use controlled inputs over uncontrolled inputs in React?

Which of the following is the best reason to prefer controlled inputs (where React state manages the input value) over uncontrolled inputs (where the DOM manages the input value)?

AControlled inputs do not need event handlers to update state.
BControlled inputs allow React to fully manage and validate input data, enabling features like instant validation and conditional disabling.
CControlled inputs require less code and are simpler to implement than uncontrolled inputs.
DControlled inputs automatically improve performance by reducing re-renders.
Attempts:
2 left
💡 Hint

Think about what control over input value enables in React apps.

Practice

(1/5)
1. What is the main purpose of using useState when handling input fields in React?
easy
A. To store and update the current value of the input field
B. To style the input field dynamically
C. To fetch data from an API when input changes
D. To prevent the input field from rendering

Solution

  1. Step 1: Understand the role of useState

    useState is a React hook used to create state variables that can hold values and update them.
  2. Step 2: Connect useState to input fields

    When handling input fields, useState stores the current input value and updates it as the user types.
  3. Final Answer:

    To store and update the current value of the input field -> Option A
  4. Quick Check:

    useState manages input value = A [OK]
Hint: useState holds input value and updates on change [OK]
Common Mistakes:
  • Thinking useState styles the input
  • Confusing useState with fetching data
  • Believing useState stops rendering
2. Which of the following is the correct way to update state when handling an input field change in React?
easy
A. onChange={() => setValue(e.target.value)}
B. onChange={(e) => setValue(e.target.value)}
C. onChange={(e) => setValue(e.value)}
D. onChange={(e) => setValue(e.target)}

Solution

  1. Step 1: Identify the event object usage

    The event object e contains the input element in e.target, and its value is in e.target.value.
  2. Step 2: Check the correct syntax for updating state

    The state update function should be called with e.target.value inside the arrow function.
  3. Final Answer:

    onChange={(e) => setValue(e.target.value)} -> Option B
  4. Quick Check:

    Use e.target.value to update state [OK]
Hint: Use e.target.value inside onChange handler [OK]
Common Mistakes:
  • Omitting event parameter e
  • Using e.value instead of e.target.value
  • Passing e.target instead of its value
3. What will be displayed when the following React component is rendered and the user types 'hello' in the input field?
import React, { useState } from 'react';

function InputExample() {
  const [text, setText] = useState('');
  return (
    <div>
      <input value={text} onChange={e => setText(e.target.value)} />
      <p>You typed: {text}</p>
    </div>
  );
}
medium
A. An input box showing 'hello' but no paragraph
B. An input box with no text and an empty paragraph
C. An input box and a paragraph showing 'You typed: hello'
D. A syntax error preventing rendering

Solution

  1. Step 1: Understand controlled input behavior

    The input's value is controlled by the state variable text. Typing updates text via setText.
  2. Step 2: Check rendered output

    The paragraph displays the current text value, so typing 'hello' shows 'You typed: hello'.
  3. Final Answer:

    An input box and a paragraph showing 'You typed: hello' -> Option C
  4. Quick Check:

    Controlled input shows typed text [OK]
Hint: Controlled input updates state and shows typed text [OK]
Common Mistakes:
  • Thinking input is uncontrolled and won't update
  • Expecting paragraph to be empty
  • Assuming syntax error without cause
4. Identify the error in this React input handler code:
function MyInput() {
  const [value, setValue] = useState('');
  function handleChange() {
    setValue(e.target.value);
  }
  return <input value={value} onChange={handleChange} />;
}
medium
A. The event parameter e is missing in handleChange
B. The value state is not initialized
C. The onChange should be onInput
D. The input should not have a value prop

Solution

  1. Step 1: Check the handleChange function signature

    handleChange uses e.target.value but does not receive e as a parameter, causing a ReferenceError.
  2. Step 2: Correct the function to accept event

    Adding e as a parameter fixes the error: function handleChange(e) { setValue(e.target.value); }.
  3. Final Answer:

    The event parameter e is missing in handleChange -> Option A
  4. Quick Check:

    Event parameter needed in handler [OK]
Hint: Always pass event parameter to onChange handler [OK]
Common Mistakes:
  • Forgetting to add event parameter e
  • Using wrong event like onInput
  • Removing value prop from controlled input
5. You want to create a form with two input fields, firstName and lastName, and update their values independently using a single useState object. Which code snippet correctly updates the state when either input changes?
const [form, setForm] = useState({ firstName: '', lastName: '' });

// Which onChange handler is correct?

A) onChange={e => setForm({ [e.target.name]: e.target.value })}

B) onChange={e => setForm(prev => ({ ...prev, [e.target.name]: e.target.value }))}

C) onChange={e => setForm({ form, [e.target.name]: e.target.value })}

D) onChange={e => setForm({ form: { [e.target.name]: e.target.value } })}
hard
A. Updates state but overwrites other fields, losing data
B. Wraps updated field inside another object, breaking state
C. Incorrectly nests state causing wrong structure
D. Correctly merges new field value with existing state

Solution

  1. Step 1: Understand state update with objects

    When updating part of an object state, you must merge the new field with existing fields to avoid losing data.
  2. Step 2: Analyze each option

    Updates state but overwrites other fields, losing data replaces the entire state with only one field, losing the other. Correctly merges new field value with existing state uses a function to merge previous state with the new field correctly. Options C and D incorrectly nest or wrap the state, breaking the structure.
  3. Final Answer:

    Correctly merges new field value with existing state -> Option D
  4. Quick Check:

    Use spread operator to merge state updates [OK]
Hint: Use spread ...prev to merge state updates in objects [OK]
Common Mistakes:
  • Overwriting entire state object losing other fields
  • Nesting state inside another object accidentally
  • Not using functional update form for safety
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