Reading and Updating Store State in Components
Access store values and actions in a React Native component using the useStore hook, and trigger updates from button presses or form submissions.
Connecting a Component to Zustand
Using a Zustand store in a component is as simple as calling the store hook at the top of the function. The hook is the same object returned by create. You can pass a selector to pick specific values, or read actions directly. No import of useSelector or useDispatch is required — the store hook handles everything.
import { useCounterStore } from '../stores/counterStore';
export default function CounterScreen() {
const count = useCounterStore((state) => state.count);
const increment = useCounterStore((state) => state.increment);
const decrement = useCounterStore((state) => state.decrement);
return (
<View style={styles.container}>
<Text style={styles.count}>{count}</Text>
<Button title='Increment' onPress={increment} />
<Button title='Decrement' onPress={decrement} />
</View>
);
}Selecting Multiple Values Efficiently
To read multiple values from a Zustand store without causing excessive re-renders, you have two options. You can call the store hook multiple times with individual selectors (each subscription is independent), or you can return an object and use the shallow equality helper to compare each property separately.
import { shallow } from 'zustand/shallow';
// Option 1: multiple hooks (preferred for 2-3 values)
const count = useCounterStore((s) => s.count);
const status = useCounterStore((s) => s.status);
// Option 2: shallow equality for multiple values at once
const { count, status } = useCounterStore(
(s) => ({ count: s.count, status: s.status }),
shallow
);All lessons in this course
- Creating a Zustand Store
- Reading and Updating Store State in Components
- Persisting Zustand State with AsyncStorage
- Slices Pattern and Devtools Integration