Complete the code to create a Zustand store with a count state initialized to 0.
import { create } from 'zustand'; const useStore = create(set => ({ count: [1], increment: () => set(state => ({ count: state.count + 1 })) }));
The count state should start at 0 to represent an initial count.
Complete the code to read the count value from the Zustand store inside a React Native component.
import React from 'react'; import { Text, Button, View } from 'react-native'; import useStore from './store'; export default function Counter() { const count = useStore(state => state.[1]); return ( <View> <Text>{count}</Text> </View> ); }
To get the current count value, select the 'count' property from the store state.
Fix the error in the code to call the increment function from the Zustand store on button press.
import React from 'react'; import { Button } from 'react-native'; import useStore from './store'; export default function IncrementButton() { const increment = useStore(state => state.[1]); return <Button title="Add" onPress={increment} />; }
The increment function is stored under 'increment' in the Zustand store and should be selected to call on press.
Fill both blanks to create a Zustand store with a boolean 'darkMode' state and a toggle function.
import { create } from 'zustand'; const useStore = create(set => ({ darkMode: [1], toggleDarkMode: () => set(state => ({ darkMode: !state.[2] })) }));
The darkMode state starts as false. The toggle function flips the current darkMode value by negating state.darkMode.
Fill all three blanks to create a Zustand store with a list of items, an addItem function, and a removeItem function.
import { create } from 'zustand'; const useStore = create(set => ({ items: [], addItem: item => set(state => ({ items: [...state.[1], [2]] })), removeItem: item => set(state => ({ items: state.items.filter(i => i !== [3]) })) }));
To add an item, spread the current 'items' array and add the new 'item'. To remove, filter out the 'item' from the array.