Complete the code to create a controlled input that updates state on change.
import React, { useState } from 'react'; function NameInput() { const [name, setName] = useState(''); return ( <input type="text" value=[1] onChange={e => setName(e.target.value)} aria-label="Name input" /> ); }
The value attribute of the input must be set to the state variable name to make it a controlled component.
Complete the code to update the state when the input changes.
import React, { useState } from 'react'; function EmailInput() { const [email, setEmail] = useState(''); return ( <input type="email" value={email} onChange=[1] aria-label="Email input" /> ); }
The onChange handler receives an event e. We use it to update the state with setEmail(e.target.value).
Fix the error in the controlled textarea component.
import React, { useState } from 'react'; function MessageInput() { const [message, setMessage] = useState(''); return ( <textarea value={message} onChange=[1] aria-label="Message input" /> ); }
The onChange handler must be a function that updates the state with the event's target value.
Fill both blanks to create a controlled checkbox that toggles state.
import React, { useState } from 'react'; function SubscribeCheckbox() { const [subscribed, setSubscribed] = useState(false); return ( <input type="checkbox" checked=[1] onChange=[2] aria-label="Subscribe checkbox" /> ); }
The checkbox's checked attribute must be linked to the state variable subscribed. The onChange handler updates the state with the checkbox's checked value.
Fill all three blanks to create a controlled select dropdown that updates state.
import React, { useState } from 'react'; function FruitSelector() { const [fruit, setFruit] = useState('apple'); return ( <select value=[1] onChange=[2] aria-label="Fruit selector" > <option value="apple">Apple</option> <option value="banana">Banana</option> <option value="orange">Orange</option> </select> ); }
The value attribute is set to the state variable fruit. The onChange handler updates the state using setFruit with the event's target value.