React interview questions
Show answer
Effects are for synchronising with something outside React (subscriptions, timers, the DOM, network). Do not use them to compute values from props or state (calculate during render or use
useMemo), to reset state when a prop changes (use akey), or to respond to a user event (do it in the event handler). Fewer effects means fewer extra renders and bugs.- intermediate
You call setCount(count + 1) three times in one click handler. What is the result and how do you fix it?
Show answer
It increases by one, because all three calls read the same
countfrom that render and updates are batched. Use the updater form,setCount(c => c + 1), which queues each update against the latest value.