Create your first store
Navigate to your project directory and run the CLI:Interactive prompts
The CLI will guide you through several prompts to configure your store:Store name
Choose a name for your store:
Store names typically follow the pattern
useStoreName (e.g., useCounterStore, useUserStore).File type
Select whether you want JavaScript or TypeScript:TypeScript stores include full type definitions and interfaces.
Persistence
Decide if you want to add state persistence:Answering “yes” adds the Zustand persist middleware, which saves your state to localStorage.
Initial state
Define your store’s initial state as JSON:Examples:
- Simple counter:
{"count":0} - User data:
{"user":{"name":"","email":""}} - Shopping cart:
{"items":[],"total":0}
Actions
Specify actions for your store (comma-separated):Examples:
increment,decrement,resetaddItem,removeItem,clearCartlogin,logout,updateProfile
Package manager
Choose your preferred package manager:The CLI uses this to install Zustand if it’s not already in your project.
Store path
Specify where to create the store file:Common paths include
store, stores, src/store, or src/stores.Example: Counter store
Let’s create a simple counter store:The generated actions include placeholder implementations with
set((state) => ({})). You’ll need to implement the actual logic for each action.What happens next
After you complete the prompts, the CLI will:- Create the store directory if it doesn’t exist
- Generate the store file with your configuration
- Check if Zustand is installed in your project
- Install Zustand automatically if needed
- Display a success message
Using your store
Once created, you can import and use your store in any component:Implementing actions
Remember to implement the actual logic for your actions. Here’s how the counter actions should look:Using saved configurations
If you saved your configuration, the CLI will automatically load it the next time you runcreate-zustand-store. You’ll see:
Next steps
Configuration
Learn about advanced configuration options
Templates
Understand the generated store templates