跪拜 Guibai
← Back to the summary

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

Last month, I took over a mid-to-back-office refactoring project that gave me a headache.

The frontend team had 4 people and wrote for two years, about 50,000+ lines of code. The tech stack looked trendy: React 18 + TypeScript + Vite + Zustand.

But in my first week, just finding where the open/close state of a certain modal was stored took me a full two hours.

I followed global searches, hopping from one Zustand store to another, and finally found it in useUIStore, crammed together with the user's login state, theme color, and table sorting state in one giant object.

The most absurd part: when I checked a row in that table with 500 rows of data, the icons in the sidebar and top bar across the whole site would flicker — a global re-render.

After refactoring for a whole month, I did one thing: I deleted 80% of the Zustand stores in the project.

The result? Code volume shrank by nearly half, those weird bugs (input losing focus, modals closing right after opening, list data getting messed up) disappeared instantly, and the page frame rate went from 30fps back to a stable 60fps.

Today I'm not going to talk about how to configure Zustand. I want to talk about how we were fooled by the concept of "global state management."

1. What you think is "decoupling" is actually "everything goes global"

Many frontend engineers (including my past self) have a misconception: as long as I introduce a global state library, I can achieve "data sharing" and "code decoupling."

So a common disaster scene appears:

The result? Your Store becomes an uncontrolled, huge "garbage bin."

After AI-assisted code writing, this problem is amplified infinitely. AI loves to import { useStore } in any component, because that's the least need to consider context passing, and it can quickly piece together functionality.

But it has no idea that a temporary state belonging to a business UI, once pushed to the global level, leads to:

  1. Extremely fragile dependencies: Component A modifies state, causing unrelated Component B to crash.
  2. Endless rendering hell: You need to manually write a bunch of shallow comparisons to prevent unnecessary re-renders.
  3. Hard-to-track bugs: Who changed isOpen? Nobody knows, because there are 15 places across the site that can modify this state.

2. The "state hell" in the AI era

I handed part of this refactoring task to Cursor and asked it to help analyze the state flow in Zustand.

AI drew me an extremely complex connection diagram. Then AI told me: "This code has serious circular dependency risk, I suggest splitting it."

I asked it: "Can you help me refactor it?" It answered: "Yes, I can help you split the Store into three sub-stores."

This is actually AI's underlying logic: use more complex architecture to cover up design flaws. It won't tell you "this state shouldn't exist globally at all," it just helps you manage it to "look better."

But in real business, the correct solution is often extremely blunt: cut it out.

3. How did I refactor? State Co-location

The core principle of the refactor is only one sentence: state should be as close as possible to where it's used.

I led the team to forcibly split those "fake global states" into three forms:

1. Component-internal state: if useState works, don't use the Store

That modal's open/close state is only used on the current page. Before: create a modalStore globally, modify globally. Now: just write const [isOpen, setIsOpen] = useState(false) at the top of the page. Done. No Store, no extra renders.

2. Locally shared state: use Context + useReducer

Some states like "table filter conditions" might need to be shared among the table component, pagination component, and chart component, but they're only used within the current page, not across routes.

Then wrap it in Context to keep the state scoped locally. AI might say Context causes re-renders, but as long as we place the Provider correctly, or combine it with useMemo, it's a million times safer than a global Store.

3. True global state: only "login state" and "global theme" remain

After sweeping through the whole project, only two states were left in Zustand: one is userToken, the other is theme.

All other business data and form data returned to where they belong.

4. Surprising gains after removing Zustand

I originally expected this refactor to cause a bunch of bugs, but testing results exceeded expectations:

5. For frontend peers lost in state management

These days on Juejin, people are constantly praising new tools and frameworks, as if not having complex state management or hand-rolling a reactive library isn't "advanced."

But in the real world, the best architecture is often the simplest architecture.

If you're using Zustand, Pinia, or Redux, I suggest you go check your Stores right now. If there are more than 10 states lying in there, ask yourself one question:

"Is this thing only used on this one page?"

If yes, steel yourself and delete it, replacing it with local useState or useReducer.

Don't let AI write that kind of "looks comprehensive, but actually nobody dares to touch" shit-mountain code.

Good architecture isn't propped up by state libraries. The real framework is your clear mental control over the data flow.