Zarządzanie stanem · Junior · Pytanie 34 z 75
Jak odczytać tylko jedno pole ze store'a Zustand?
const useStore = create((set) => ({ count: 0, user: null, inc: () => {} }));- ✓Poprawna odpowiedź:
const count = useStore((s) => s.count) - 2
const { count } = useContext(useStore) - 3
const count = useStore.count
Wyjaśnienie
Selektor subskrybuje komponent tylko na count: zmiany user nie wywołają jego renderu. Wywołanie useStore() bez selektora subskrybuje cały store i renderuje przy każdej zmianie.