const useTodoStore = create((set) => ( todos: [], fetchTodos: async () => const response = await fetch('https://jsonplaceholder.typicode.com/todos') const data = await response.json() set( todos: data ) , addTodo: (title) => set((state) => ( todos: [...state.todos, id: Date.now(), title, completed: false ] )) )) For large apps, split your store:
Zustand is not tied to React. You can use it in vanilla JS: zust4help full
| Feature | Zustand | Redux Toolkit | Context + useReducer | |---------|---------|---------------|----------------------| | Boilerplate | Very low | Medium | High | | Provider required | No | Yes | Yes | | DevTools support | Yes (middleware) | Yes | No | | Async actions | Native | Thunk/Saga | Manual | | Performance | Excellent | Good | Poor (re-renders) | | Learning curve | Minimal | Steep | Moderate | | Bundle size | ~1.5 kB | ~15 kB | ~0 kB (built-in) | Part 8: Real-World Example – Complete E-Commerce Cart import create from 'zustand' import persist from 'zustand/middleware' import shallow from 'zustand/shallow' const useCartStore = create( persist( (set, get) => ( items: [], addItem: (product) => set((state) => const existing = state.items.find((i) => i.id === product.id) if (existing) return items: state.items.map((i) => i.id === product.id ? ...i, quantity: i.quantity + 1 : i ), const useTodoStore = create((set) => ( todos: [],
For React integration:
// Dispatch actions store.getState().increment() console.log(store.getState().count) // 1 const useTodoStore = create((set) =>