0
0
Reactframework~20 mins

Form handling in React - Practice Problems & Coding Challenges

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