A value you can work out: keep it in state + useEffect, or work it out during render? On the neighbourhood head’s board the cast is read from the real DOM, so…
Why it matters
"Copy props into state, then sync it with useEffect" is one of the most common React bugs: stale values, extra renders, missed deps, even an endless loop. Knowing "work it out during render, don’t store it" wipes out the whole class of bugs.
The key idea
If it can be worked out from other state or props, don’t store new state
Common mistakes
useEffect to copy state into state One render late, an extra render, easily stale. Just work it out during render: const x = hitung(a, b).
A derived state becoming a second source of truthuseState(props.x) reads the props on the first render only. Two states that must "always match" will differ one day: store one, work out the rest.
useMemo everywhere "to be fast" useMemo is only for costly calculations. For a small list filter, work it out directly during render: simpler and just as right.
A copying effect with no deps: endless renders With no deps an effect runs after every render, and setState with a new array or object always starts the next render. React’s development build warns "Maximum update depth exceeded". Give it deps, or drop the effect.
Try it in this playground
useState(castOf): pick another play The title is new, the cast is old: the copy in state is stale.
useState + useEffect: pick again Two renders, and the first commit showed the old cast for a moment.
castOf(lakon) during render One render, never stale, no state.
useMemo: strike the gong Another render with the same play: castOf does not run again.