Bird
Raised Fist0
Reactframework~20 mins

Form handling in React - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
React Form Mastery
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 form component?
Consider this React component that handles a simple text input form. What will be displayed below the input after typing 'hello'?
React
import React, { useState } from 'react';

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

  return (
    <form>
      <label htmlFor="nameInput">Name:</label>
      <input
        id="nameInput"
        type="text"
        value={name}
        onChange={e => setName(e.target.value)}
        aria-label="Name input"
      />
      <p>Hello, {name}!</p>
    </form>
  );
}

export default GreetingForm;
AThe page shows an input box but the text below remains empty regardless of typing.
BThe page shows an input box and below it the text 'Hello, hello!' after typing 'hello'.
CThe page shows an input box and below it the text 'Hello, !' always, ignoring input.
DThe page crashes with a runtime error when typing in the input.
Attempts:
2 left
💡 Hint
Think about how the state updates on input change and how it reflects in the paragraph.
📝 Syntax
intermediate
1:30remaining
Which option correctly prevents the default form submission behavior?
In React, to stop a form from refreshing the page on submit, which code snippet correctly prevents the default event?
React
function MyForm() {
  const handleSubmit = (event) => {
    // What goes here?
  };

  return (
    <form onSubmit={handleSubmit}>
      <button type="submit">Submit</button>
    </form>
  );
}
Areturn false;
Bevent.stopPropagation();
Cevent.defaultPrevented = true;
Devent.preventDefault();
Attempts:
2 left
💡 Hint
Look for the standard method to stop default browser actions.
🔧 Debug
advanced
2:30remaining
Why does this React form input not update on typing?
This React component renders an input but typing does not change the input value. What is the cause?
React
import React, { useState } from 'react';

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

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

export default FixedInput;
AThe input type should be 'controlled' instead of 'text' to update.
BThe input is missing a name attribute, so React ignores changes.
CThe onChange handler sets the state to the old value, so input never updates.
DThe useState hook is not initialized properly with an empty string.
Attempts:
2 left
💡 Hint
Check what value is passed to setText inside onChange.
❓ state_output
advanced
2:00remaining
What is the final state value after submitting this form?
Given this React component, what is the value of submittedName after typing 'Anna' and clicking submit?
React
import React, { useState } from 'react';

function SubmitName() {
  const [name, setName] = useState('');
  const [submittedName, setSubmittedName] = useState('');

  const handleSubmit = (e) => {
    e.preventDefault();
    setSubmittedName(name);
    setName('');
  };

  return (
    <form onSubmit={handleSubmit}>
      <input
        type="text"
        value={name}
        onChange={e => setName(e.target.value)}
        aria-label="name input"
      />
      <button type="submit">Submit</button>
      <p>Submitted: {submittedName}</p>
    </form>
  );
}

export default SubmitName;
A'Anna'
B'' (empty string)
Cundefined
Dnull
Attempts:
2 left
💡 Hint
Look at what value is saved to submittedName on submit.
🧠 Conceptual
expert
2:30remaining
Which statement about controlled vs uncontrolled components in React forms is true?
Select the correct statement about controlled and uncontrolled form components in React.
AControlled components store form data in React state and update on every input change.
BUncontrolled components require React state to update input values on every keystroke.
CControlled components use refs to access form values directly from the DOM.
DUncontrolled components prevent default form submission automatically.
Attempts:
2 left
💡 Hint
Think about where the form data lives in controlled vs uncontrolled inputs.

Practice

(1/5)
1. What is the main purpose of using useState in React form handling?
easy
A. To automatically validate form inputs
B. To style the form elements dynamically
C. To send form data directly to the server
D. To store and update the current value of form inputs

Solution

  1. Step 1: Understand useState role in React

    useState is used to create a state variable that holds data and can be updated.
  2. Step 2: Connect useState to form inputs

    In form handling, useState stores the current input value and updates it on user typing.
  3. Final Answer:

    To store and update the current value of form inputs -> Option D
  4. Quick Check:

    useState manages input values = A [OK]
Hint: useState holds input values and updates on change [OK]
Common Mistakes:
  • Thinking useState styles elements
  • Assuming useState sends data to server
  • Confusing useState with validation logic
2. Which of the following is the correct way to update state on input change in React?
easy
A. onChange={(e) => setValue(e.target.value)}
B. onChange={() => setValue(e.value)}
C. onChange={(e) => setValue(e.target)}
D. onChange={(e) => setValue(e.currentTarget)}

Solution

  1. Step 1: Identify correct event target usage

    The input value is accessed via e.target.value in event handlers.
  2. Step 2: Confirm correct syntax for setState call

    Using setValue(e.target.value) updates state with the input's current value.
  3. Final Answer:

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

    Use e.target.value to get input value [OK]
Hint: Use e.target.value inside onChange to update state [OK]
Common Mistakes:
  • Using e.value instead of e.target.value
  • Passing e.target or e.currentTarget directly
  • Omitting the event parameter e
3. What will be the output when the following React component is rendered and the user types 'Hi' in the input?
function GreetingForm() {
  const [name, setName] = React.useState('');
  return (
    <form>
      <input value={name} onChange={e => setName(e.target.value)} />
      <p>Hello, {name}!</p>
    </form>
  );
}
medium
A. The page reloads on typing
B. The paragraph shows 'Hello, Hi!' as user types 'Hi'
C. The paragraph remains empty regardless of input
D. The input clears automatically after typing

Solution

  1. Step 1: Understand controlled input behavior

    The input value is controlled by state variable 'name' which updates on change.
  2. Step 2: Observe paragraph rendering with state

    The paragraph displays 'Hello, {name}!' so it updates live as 'name' changes.
  3. Final Answer:

    The paragraph shows 'Hello, Hi!' as user types 'Hi' -> Option B
  4. Quick Check:

    Controlled input updates state and display = D [OK]
Hint: Controlled inputs update state and display live [OK]
Common Mistakes:
  • Expecting input to clear automatically
  • Thinking paragraph won't update
  • Assuming page reloads on input change
4. Identify the error in this React form submission handler:
function handleSubmit(e) {
  alert('Submitted');
}

What is missing to prevent the page from reloading on submit?
medium
A. Call e.preventDefault() inside handleSubmit
B. Use return false at the end of handleSubmit
C. Wrap alert in a setTimeout function
D. No error, the code works fine

Solution

  1. Step 1: Understand default form submit behavior

    By default, form submission reloads the page unless prevented.
  2. Step 2: Prevent default reload in React

    Calling e.preventDefault() inside the submit handler stops the reload.
  3. Final Answer:

    Call e.preventDefault() inside handleSubmit -> Option A
  4. Quick Check:

    Use e.preventDefault() to stop reload [OK]
Hint: Always call e.preventDefault() in submit handlers [OK]
Common Mistakes:
  • Forgetting to pass event parameter e
  • Using return false instead of preventDefault
  • Assuming alert stops reload
5. You want to handle multiple input fields in one React form using a single state object. Which code snippet correctly updates the state for the changed input field?
const [formData, setFormData] = React.useState({ email: '', password: '' });

function handleChange(e) {
  // What goes here?
}
hard
A. setFormData({ e.target.name: e.target.value });
B. setFormData({ formData[e.target.name]: e.target.value });
C. setFormData({ ...formData, [e.target.name]: e.target.value });
D. setFormData({ [e.target.value]: e.target.name });

Solution

  1. Step 1: Understand updating nested state objects

    To update one field in an object state, copy existing fields with spread operator.
  2. Step 2: Use computed property name for dynamic key

    Use [e.target.name] to dynamically update the correct field with new value.
  3. Final Answer:

    setFormData({ ...formData, [e.target.name]: e.target.value }); -> Option C
  4. Quick Check:

    Spread old state + dynamic key update = B [OK]
Hint: Use spread and [e.target.name] to update form object state [OK]
Common Mistakes:
  • Overwriting entire state without spreading
  • Using invalid object key syntax
  • Swapping name and value in update
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