0
0
Reactframework~20 mins

Handling input fields in React - Practice Problems & Coding Challenges

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