State Management · Junior · Question 7 of 75
Should you store fullName in state if you already have firstName and lastName?
- ✓Correct answer: No - compute it during render:
const fullName = firstName + ' ' + lastName - 2Yes - and sync it with a
useEffecton every change - 3Yes - otherwise React can't display the computed value
Explanation
Redundant state leads to desynchronization and extra renders. Anything derivable from props and existing state is computed during render. If the computation is expensive, wrap it in useMemo.