Why Go Beyond Components?
Until now we said state should be as close as possible to where it's used. But imagine a shopping cart in an online store. It's needed:
- In the site header (product counter).
- On the product page ( " Add " button).
- In the cart sidebar.
- On the checkout page.
If you try to " thread " the product list through props from the very top to the very bottom, your code will turn into a tangled knot of threads.
1. Analogy: Global Cloud
- Local State (
useState): These are your personal thoughts. No one knows about them until you say. - Global State: This is a common chat in Telegram or cloud storage. Any component (any " employee " ) can at any moment look into this cloud, take data or update it. And everyone else will instantly see changes.
2. Zustand: Modern Standard
For a long time, Redux dominated React — powerful but very complex and cumbersome. Zustand (German for " state " ) replaced it.
- Simplicity: You create a store in literally 10 lines of code.
- Scalability: It works equally well in small business cards and huge CRM systems.
- Performance: Only components that read a specific field ( " product count " ) will be updated. The rest of the site will continue " sleeping " .
3. Golden Rule: Where to Store Data?
Don't shove everything into global state. This is a typical beginner mistake.
- Local State (
useState): Data needed only here and now (text in input field, is dropdown open). - Server State (React Query): Data that came from the database (post list, user profile).
- Global State (Zustand): Purely client data needed by the entire application (theme, cart, auth token).
Global state is your application's coordination center. It makes architecture predictable and clean.