React · Middle · Question 14 of 75
What's the right way to get fullName from firstName and lastName stored in state?
- 1A separate
fullNamestate and auseEffectthat updates it - ✓Correct answer: Compute it during render:
const fullName = firstName + ' ' + lastName - 3Keep
fullNamein auseRefand update it manually
Explanation
Values that can be computed from props and state shouldn't be stored in state. The "state + syncing effect" combo causes an extra render with a stale value and a risk of desync. If the computation is expensive, wrap it in useMemo.