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.