Build: Counter with Zustand
A simple counter app screen using Zustand for state management instead of React's useState.
Target UI
Counter with Zustand +---------------------+ | Counter | | | | 0 | | | | [+] [-] | +---------------------+
Counter with Zustand +---------------------+ | Counter | | | | 0 | | | | [+] [-] | +---------------------+
import React from 'react'; import { View, Text, Button, StyleSheet } from 'react-native'; import create from 'zustand'; // TODO: Create Zustand store here export default function CounterScreen() { // TODO: Use Zustand store here return ( <View style={styles.container}> <Text style={styles.title}>Counter with Zustand</Text> <Text style={styles.count}>0</Text> <View style={styles.buttons}> <Button title="+" onPress={() => {}} /> <Button title="-" onPress={() => {}} /> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center' }, title: { fontSize: 24, marginBottom: 20 }, count: { fontSize: 48, marginBottom: 20 }, buttons: { flexDirection: 'row', width: '40%', justifyContent: 'space-between' } });
import React from 'react'; import { View, Text, Button, StyleSheet } from 'react-native'; import create from 'zustand'; const useStore = create(set => ({ count: 0, increment: () => set(state => ({ count: state.count + 1 })), decrement: () => set(state => ({ count: state.count - 1 })) })); export default function CounterScreen() { const { count, increment, decrement } = useStore(); return ( <View style={styles.container}> <Text style={styles.title}>Counter with Zustand</Text> <Text style={styles.count}>{count}</Text> <View style={styles.buttons}> <Button title="+" onPress={increment} /> <Button title="-" onPress={decrement} /> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center' }, title: { fontSize: 24, marginBottom: 20 }, count: { fontSize: 48, marginBottom: 20 }, buttons: { flexDirection: 'row', width: '40%', justifyContent: 'space-between' } });
We created a Zustand store using create with a count state and two actions: increment and decrement. These actions update the count by adding or subtracting 1.
In the CounterScreen component, we use the store by calling useStore() to get the current count and the two actions.
The UI shows the count in a large font. The '+' and '-' buttons call the increment and decrement actions respectively when pressed.
This setup keeps state management simple and global without needing React's useState or context, demonstrating Zustand as a lightweight alternative.
Counter with Zustand +---------------------+ | Counter | | | | 5 | | | | [+] [-] | +---------------------+
const useStore = create(set => ({ count: 0, increment: () => set(state => ({ count: state.count + 1 })) }))
function Counter() {
const count = useStore(state => state.count)
const increment = useStore(state => state.increment)
increment()
return {count}
}Counter renders?const useStore = create(set => ({
count: 0,
increment: () => set({ count: count + 1 })
}))const useStore = create(set => ({
count: 0,
increment: () => set(state => ({ count: state.count + 1 })),
reset: ???
}))state.count directly in set({ ... }) but state is undefined there (error). () => set({ count: 0 }) correctly sets { count: 0 } directly.Pass the test and this pattern moves unknown