Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Selectors and Derived State | Structuring and Using Zustand State
State Management in React with Zustand

bookSelectors and Derived State

Selectors in Zustand are functions that allow you to pick or compute specific pieces of state from your store. Instead of accessing the entire store every time, you can use a selector to subscribe only to the part of the state your component needs. This approach offers important performance benefits: components will only re-render when the selected piece of state changes, not when unrelated parts of the store update. This makes your React app more efficient, especially as your state grows.

You can use selectors to access simple values, but they are also powerful for creating derived state. Derived state is any value that is calculated from existing state rather than stored directly. For example, you might want to display the number of items in a list or determine whether a counter has reached a certain threshold. Instead of storing these computed values in your store, you can derive them on the fly using selectors.

To use a selector in a component, you pass a function to your store hook. This function receives the entire state and returns only the value you need. For instance, if your state has a count property, you can write:

useCounterStore(state => state.count);
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookSelectors and Derived State

Swipe um das Menü anzuzeigen

Selectors in Zustand are functions that allow you to pick or compute specific pieces of state from your store. Instead of accessing the entire store every time, you can use a selector to subscribe only to the part of the state your component needs. This approach offers important performance benefits: components will only re-render when the selected piece of state changes, not when unrelated parts of the store update. This makes your React app more efficient, especially as your state grows.

You can use selectors to access simple values, but they are also powerful for creating derived state. Derived state is any value that is calculated from existing state rather than stored directly. For example, you might want to display the number of items in a list or determine whether a counter has reached a certain threshold. Instead of storing these computed values in your store, you can derive them on the fly using selectors.

To use a selector in a component, you pass a function to your store hook. This function receives the entire state and returns only the value you need. For instance, if your state has a count property, you can write:

useCounterStore(state => state.count);
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1
some-alt