Skip to main content

Handle state

NameWhen to use it
URLSharable app location
Web storagePersistent between sessions, one browser
Local stateOnly one component needs the state
Lifted stateA few related components need the state
Derived stateState can be derived from existing state
RefsDOM reference, state that isn't rendered
ContextGlobal or subtree state
Third party libraryGlobal state, Remote sate (redux or zustand)

Derived state

Derived state is state that is derived from other state. For example, if you have a firstName and a lastName state, you can derive a fullName state from them.

❌ Avoid

const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [fullName, setFullName] = useState('');

✅ Prefer

const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const fullName = `${firstName} ${lastName}`;