Consider the following code for a React component:
function Counter() { const [count, setCount] = useState(0); function increment() { setCount(count + 1); } return ( <div> Count: {count} <button onClick={increment}>+</button> </div> ); }
What happens when the "+" button is clicked twice in quick succession?
The increment function fails because count is immutable.
count
The count increases by 2 immediately because React batches the updates.
The component re-renders twice, once after each click, showing 1 then 2.
The count increases by 1 each time, showing 2 after the second click.
The count stays at 0 because the state update is stale.
Here’s a buggy React component:
function NameList() { const [names, setNames] = useState(["Alice"]); function addName() { names.push("Bob"); setNames(names); } return ( <div> {names.join(", ")} <button onClick={addName}>Add Bob</button> </div> ); }
Why doesn’t "Bob" appear in the UI after clicking the button?
The setNames function is called with an invalid value.
setNames
The button’s onClick event isn’t properly bound to addName.
onClick
addName
React only re-renders when primitive values (like numbers) change, not arrays.
The push method doesn’t return a new array, so React doesn’t detect a state change.
push
You’re tasked with fixing the NameList component from Question #2. Which of these solutions will make "Bob" appear after clicking the button?
NameList
setNames(names.push("Bob"))
setNames(names.slice().push("Bob"))
setNames([...names, "Bob"])
setNames(names.concat(["Bob"]))
Examine this component managing an object in state:
function Profile() { const [user, setUser] = useState({ name: "Eve", age: 25 }); function birthday() { user.age += 1; setUser(user); } return ( <div> {user.name}, Age: {user.age} <button onClick={birthday}>+1 Age</button> </div> ); }
What’s wrong with this code, and how does it affect the UI?
The object reference doesn’t change, so React doesn’t re-render.
setUser can’t handle objects, only primitive values.
setUser
Nothing is wrong; the age will increase as expected.
The birthday function needs to use a producer function for objects.
birthday
How could you fix the Profile component from Question #4 to correctly update the age? (Select all that apply.)
Profile
setUser(user.age += 1)
setUser(Object.assign({}, user, { age: user.age + 1 }))
setUser({ ...user, age: user.age + 1 })
setUser((prev) => ({ ...prev, age: prev.age + 1 }))
Consider this custom hook and component:
function useCounter() { const [count, setCount] = useState(0); return { count, increment: () => setCount(count + 1) }; } function App() { const { count, increment } = useCounter(); return ( <div> Count: {count} <button onClick={increment}>+</button> </div> ); }
What’s true about this code?
It works fine because useCounter is called within App, a functional component.
useCounter
App
It violates the rule that hooks can only be called inside functional components.
The increment function will fail because it doesn’t use a producer function.
increment
The state will reset to 0 on every render because useCounter is called repeatedly.
Here’s a component with conditional hook usage:
function Toggle() { const [isOn, setIsOn] = useState(false); if (isOn) { const [value, setValue] = useState(10); return <div>{value}</div>; } return <button onClick={() => setIsOn(true)}>Turn On</button>; }
What happens when you click the "Turn On" button?
The component re-renders infinitely.
The value state is ignored, and the button stays visible.
value
The component renders 10 as expected.
React throws an error because hooks are conditionally called.
Look at this batching example:
function BatchCounter() { const [a, setA] = useState(0); const [b, setB] = useState(0); function updateBoth() { setA(a + 1); setB(b + 1); console.log(a, b); } return ( <div> A: {a}, B: {b} <button onClick={updateBoth}>Update</button> </div> ); }
What will the console log show after clicking the button once, assuming initial values are 0?
0, 1
1, 1
0, 0
1, 0
You’re building a todo list app. Here’s a snippet:
function TodoList() { const [todos, setTodos] = useState(["Buy milk"]); function addTodo() { setTodos([...todos, "Call mom"]); } function removeLastTodo() { todos.pop(); setTodos(todos); } return ( <div> {todos.join(", ")} <button onClick={addTodo}>Add</button> <button onClick={removeLastTodo}>Remove Last</button> </div> ); }
What happens when you click "Remove Last" after adding "Call mom"?
"Call mom" is removed from the UI.
The component crashes because pop isn’t allowed with state.
pop
The UI shows only "Buy milk" because pop triggers a re-render.
Nothing changes in the UI because the array reference stays the same.
How could you fix the removeLastTodo function from Question #9 to correctly remove the last todo? (Select all that apply.)
removeLastTodo
setTodos(todos.slice(0, -1))
setTodos(todos.filter((_, i) => i !== todos.length - 1))
setTodos(todos.pop())
setTodos([...todos.slice(0, -1)])
This content is only available to registered users.Please register at the the home page.