Complete the code to create a state variable in a React functional component.
const [count, [1]] = useState(0);
In React, the convention is to name the setter function as set plus the state variable name. Here, setCount is the correct setter for count.
Complete the code to lift the state up by passing the state setter as a prop.
<ChildComponent onChange=[1] />To lift state up, you pass the setter function setCount to the child component so it can update the parent's state.
Fix the error in the code by choosing the correct way to update the lifted state in the child component.
function Child({ onChange }) {
return <button onClick={() => [1](prev => prev + 1)}>Increment</button>;
}The child component receives the setter function as onChange. To update the state, call onChange with the new value or updater function.
Fill both blanks to correctly lift state up and pass the current state as a prop.
function Parent() {
const [value, [1]] = useState('');
return <Child value=[2] onChange={setValue} />;
}The setter function is named 'setValue' and the current state is 'value'. Both must be used correctly to lift state and pass it down.
Fill all three blanks to complete a lifted state example with a controlled input.
function Parent() {
const [text, [1]] = useState('');
return <Child inputValue=[2] onInputChange=[3] />;
}
function Child({ inputValue, onInputChange }) {
return <input value={inputValue} onChange={e => onInputChange(e.target.value)} />;
}The state setter is 'setText', the current state is 'text', and the setter is passed as 'onInputChange' to update the state from the child.