Before diving into advanced topics, let's understand the problem Zustand solves.
In React, sharing state between components that aren't directly related is painful. You either "prop drill" data down many layers, or you use the Context API — which re-renders every subscriber whenever anything changes. Zustand provides a global store that is:
# Install Zustand
npm install zustand
**A minimal Zustand store:**
```js
import { create } from 'zustand'
const useCounterStore = create((set) => ({
count: 0,
increment: () => set((state) => ({ count: state.count + 1 })),
decrement: () => set((state) => ({ count: state.count - 1 })),
}))
// In a component:
function Counter() {
const count = useCounterStore((state) => state.count)
const increment = useCounterStore((state) => state.increment)
return <button onClick={increment}>Count: {count}</button>
}
That's it. No Provider wrapping, no dispatch, no action types.
---