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"?
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" /> ); }
Think about how the value and onChange props work together in controlled inputs.
The component uses React state name to store the input value. The onChange handler updates this state with the typed characters. Because the input's value is set to name, the input box shows exactly what the user types.
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?
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} /> </> ); }
Remember to keep the other fields unchanged when updating one field in an object state.
Option A uses the functional form of setForm and spreads the previous state to keep other fields intact, then updates only the changed field. Other options either overwrite the entire state or use incorrect syntax.
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?
function TextInput() { const [text, setText] = React.useState(''); return ( <input type="text" value={text} onChange={e => setText} aria-label="Text input" /> ); }
Check how the onChange handler updates the state.
The onChange handler passes the function setText itself instead of calling it with the new value. It should be onChange={e => setText(e.target.value)} to update the state properly.
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"?
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" /> ); }
Look at how the input value is transformed before setting state.
The handleChange function converts the input value to uppercase before updating the state. So typing "hi" results in the state being "HI".
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)?
Think about what control over input value enables in React apps.
Controlled inputs let React keep the source of truth for input values, which allows easy validation, conditional UI changes, and synchronization with other state. Other options are incorrect because controlled inputs often require more code and event handlers, and do not inherently improve performance.