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'?
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> ); }
Remember that event.preventDefault() stops the page from reloading and the input state updates on every change.
The input state updates as the user types. When the form submits, handleSubmit prevents the default reload and logs the current input value, which is 'hello'.
In React, to stop a form from reloading the page on submit, which code snippet correctly prevents the default behavior?
function handleSubmit(event) { // prevent default here console.log('Submitted'); }
Check the exact spelling and casing of the method to prevent default browser behavior.
The correct method is event.preventDefault() with capital D and capital P. Other options are either misspelled or do different things.
Given this React component, what will be the value of submittedData after the user types 'abc' and submits the form?
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> ); }
Remember that setSubmittedData is called before clearing the input state.
The submittedData state is set to the current input value 'abc' before the input is cleared, so it holds 'abc' after submit.
Look at this React form component. The page reloads on submit even though event.preventDefault() is called. What is the cause?
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> ); }
Check the exact spelling and casing of the method used to stop default behavior.
The method is case sensitive. Using event.preventdefault() (lowercase d) is undefined, so the default submit happens causing reload.
In React, when handling form submissions, why do we call event.preventDefault() inside the submit handler?
Think about what happens by default when a form submits in a browser.
By default, form submission reloads the page which resets React state. Calling event.preventDefault() stops this reload so React can handle data and state smoothly.