跪拜 Guibai
← All articles
Frontend

Deleting 80% of Zustand Stores Cut Code in Half and Fixed Bugs

By 涛涛ing ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Global state libraries are a reflex in React projects, but this refactor shows a measurable cost: redundant renders, fragile dependencies, and unreadable code. Co-locating state with local hooks and Context can deliver better performance and maintainability without abandoning the ecosystem.

Summary

A frontend engineer on a two-year-old mid-office project found that Zustand stores had become an uncontrolled global garbage bin. One modal's visibility state was buried in `useUIStore` alongside login state, theme, and table sorting. Checking a row in a 500-row table triggered a full-site re-render, and mysterious bugs (input losing focus, modals closing instantly) were impossible to trace because 15 places could mutate `isOpen`.

The fix was state co-location: keep state as close to its usage as possible. The modal switch became a local `useState`, page-level shared state like table filters moved to Context + useReducer, and only `userToken` and `theme` stayed in Zustand. The result: code volume dropped by half, bugs disappeared, and frame rate stabilized at 60fps. The author also notes that AI coding assistants amplify the problem—they love `import { useStore }` in any component, and they suggest splitting stores into more sub-stores rather than questioning whether the state belongs globally at all.

Takeaways
— A 50,000-line React project with 4 developers used Zustand for nearly everything, including modal visibility, form values, and loading states.
— Any global state change can cause unrelated components to re-render, leading to full-site flicker and frame drops from 60 to 30 fps.
— AI assistants often suggest splitting a store into more sub-stores instead of removing global state altogether, masking design flaws with extra complexity.
— State co-location means placing state as close as possible to its consumer: local `useState` for component-only state, Context + useReducer for page-scoped shared state.
— After refactoring, only `userToken` and `theme` needed to stay in Zustand; all other business and form state moved back into components or page context.
— Removing 80% of stores halved code volume, eliminated specific bugs, and made unit testing simpler since components only need props instead of heavy store mocks.
Conclusions

The real culprit isn't Zustand itself but the habit of promoting any temporary UI state to global scope; the library only makes that habit frictionless.

AI coding tools amplify over-globalization because they optimize for easy feature assembly, not for architectural cleanliness.

A store with more than ten unrelated states is a strong smell that it's become a dumping ground; local hooks or Context often solve the problem with far less overhead.

The performance win here came not from optimizing renders but from eliminating the global render trigger altogether.

Concepts & terms
State Co-location
An architecture principle that places state as close as possible to the component or page that uses it, instead of lifting everything into a global store. Reduces dependencies, avoids unnecessary re-renders, and makes data flow explicit.
Zustand
A small React state management library based on hooks. Popular for its minimal API, but easy to misuse by putting all shared or even page-local state into global stores.
Global State
Data that is accessible from anywhere in an application, often managed by a store like Zustand, Redux, or Pinia. When overused, it creates coupling and performance problems.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗