0
0
Reactframework~20 mins

Form submission handling in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this React form is submitted?

Consider this React component that handles a form submission. What will be the output in the console when the form is submitted with the input value 'hello'?

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

export default function MyForm() {
  const [input, setInput] = useState('');

  function handleSubmit(event) {
    event.preventDefault();
    console.log(input);
  }

  return (
    <form onSubmit={handleSubmit} aria-label="simple form">
      <input
        type="text"
        value={input}
        onChange={e => setInput(e.target.value)}
        aria-label="text input"
      />
      <button type="submit">Submit</button>
    </form>
  );
}
AThe console logs an empty string ''.
BThe console logs 'hello' exactly.
CThe console logs 'undefined'.
DThe console logs an error because event.preventDefault() is missing.
Attempts:
2 left
💡 Hint

Remember that event.preventDefault() stops the page from reloading and the input state updates on every change.

📝 Syntax
intermediate
1:30remaining
Which option correctly prevents the default form submission behavior?

In React, to stop a form from reloading the page on submit, which code snippet correctly prevents the default behavior?

React
function handleSubmit(event) {
  // prevent default here
  console.log('Submitted');
}
Aevent.preventdefault()
Bevent.defaultPrevented = true
Cevent.stopPropagation()
Devent.preventDefault()
Attempts:
2 left
💡 Hint

Check the exact spelling and casing of the method to prevent default browser behavior.

state_output
advanced
2:00remaining
What is the value of 'submittedData' after submitting this form?

Given this React component, what will be the value of submittedData after the user types 'abc' and submits the form?

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

export default function DataForm() {
  const [input, setInput] = useState('');
  const [submittedData, setSubmittedData] = useState(null);

  function handleSubmit(e) {
    e.preventDefault();
    setSubmittedData(input);
    setInput('');
  }

  return (
    <form onSubmit={handleSubmit} aria-label="data form">
      <input
        value={input}
        onChange={e => setInput(e.target.value)}
        aria-label="data input"
      />
      <button type="submit">Send</button>
      <p>Last submitted: {submittedData}</p>
    </form>
  );
}
A'' (empty string)
Bnull
C'abc'
Dundefined
Attempts:
2 left
💡 Hint

Remember that setSubmittedData is called before clearing the input state.

🔧 Debug
advanced
2:30remaining
Why does this form submit reload the page despite calling preventDefault?

Look at this React form component. The page reloads on submit even though event.preventDefault() is called. What is the cause?

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

export default function BuggyForm() {
  const [text, setText] = useState('');

  function handleSubmit(event) {
    event.preventDefault();
    console.log(text);
  }

  return (
    <form onSubmit={handleSubmit} aria-label="buggy form">
      <input value={text} onChange={e => setText(e.target.value)} aria-label="bug input" />
      <button type="submit">Go</button>
    </form>
  );
}
AThe method preventdefault() is misspelled; it should be preventDefault().
BThe input value is not controlled properly, causing reload.
CThe form lacks a name attribute, causing default submit.
DThe button type should be 'button' instead of 'submit'.
Attempts:
2 left
💡 Hint

Check the exact spelling and casing of the method used to stop default behavior.

🧠 Conceptual
expert
1:30remaining
Why is it important to call event.preventDefault() in React form submissions?

In React, when handling form submissions, why do we call event.preventDefault() inside the submit handler?

ATo stop the browser from reloading the page and losing React state on submit.
BTo clear the form inputs automatically after submission.
CTo validate the form inputs before sending data.
DTo enable the form to submit data to the server asynchronously.
Attempts:
2 left
💡 Hint

Think about what happens by default when a form submits in a browser.