r/reactjs • u/CaptainZiboo • 15d ago
Discussion How do you structure feature-based React apps with Zustand, TanStack Query, providers, and business logic?
Hey everyone! π
I'm trying to establish a clean, reusable feature-based architecture for React/TypeScript apps, especially when using Zustand + TanStack Query.
I'm working on a visual editor that fetches documents from an API, initializes a Zustand editing session, and will eventually handle canvas state, selection, history, saving, etc.
I'm considering this structure:
features/editor/
βββ components/
βββ hooks/
βββ store/
β βββ editor-store.ts
β βββ editor-store-provider.tsx
β βββ slices/
β βββ document-slice.ts
β βββ selection-slice.ts
β βββ history-slice.ts
βββ queries/
βββ domain/ ?
βββ utils/ ?
βββ lib/ ?
βββ services/ ?I'd love to know how you handle a few things in production:
- API / queries: Where do raw API functions go? Do you separate them from TanStack Query options/hooks? Do you organize queries by feature or shared domain?
- Zustand: Do you keep providers alongside stores? When do you introduce slices vs multiple stores?
Business/domain logic vs utilities: This is probably what I'm most unsure about. Where do you put pure functions containing actual application logic, independently of React, Zustand or API calls? For example: Would a
domain/directory make sense here? For example,domain/geometry.tsfor canvas calculations,domain/document.tsfor document manipulation, etc. Or would you simply useutils/,lib/orservices/? How do you distinguish business/domain logic from generic utility functions? Is a dedicateddomain/folder a useful convention, or does it introduce unnecessary complexity?- Formatting an aspect ratio.
- Calculating canvas geometry, scaling or snapping.
- Validating editor-specific constraints.
- Transforming or manipulating document data.
- Shared code: When do you move something out of a feature into a shared location? Do you prefer shared domain modules or global utilities?
- Server vs local state: Would you fetch with TanStack Query, initialize Zustand from the result, then keep local edits separate from the query cache? How do you avoid overwriting unsaved changes on refetch?
I'm not looking for a strict architecture or lots of abstractions, just practical conventions that stay maintainable as projects grow.
If you have real-world folder structures or examples, I'd love to see them. Thanks! π
17
u/Cordes96 15d ago edited 15d ago
I separate them out into query keys so that I can re use the query keys across the application without needing to import hooks, and also it allows me to invalidate queries without having to do anything extra beyond importing the query keys across features, which end up in a shared api folder either in a package or in shared/api to avoid cross-feature imports and the hooks stay in the feature.
These are my opinions, but I have 8 YoE as a software architect/consultant on large frontend monorepos, and these are the most scalable methods I have found without getting super overcomplicated or re inventing the wheel.
1
u/GeekyAntsBFSI 14d ago
A practical starting point:
domain/Β holds pure editor rules;Βutils/Β holds generic helpers. Keep providers beside stores, add slices when needed, and share code once another feature needs it.TanStack Query owns server data; Zustand owns the editing draft. Initialize once per document session. Refetches should flag newer versions, never overwrite unsaved edits.
1
u/NopeusGT 12d ago
FSD
1
u/rsajdok 11d ago
There is no better example: https://martinfowler.com/articles/modularizing-react-apps.html
1
u/DVR_Design 14d ago
For the unsaved changes part, I'd treat the query cache as the last saved snapshot and keep the Zustand session as a separate draft. Initialize the store once per document id (key the provider on the id), and on refetch only rehydrate if the draft isn't dirty, otherwise hold the new server version aside and show a "this changed on the server" prompt. Keep a baseVersion or updatedAt in the store so a save can be rejected or merged when the server moved underneath you. Setting a high staleTime and turning off refetchOnWindowFocus for the editor query also removes most surprise refetches. On folders, a domain/ dir for pure stuff like geometry and snapping is worth it mostly because those are the easiest things to unit test without React.
1
u/Mamad-mirza-dev 5d ago
The structure you have is close β I'd just draw the lines a bit harder. 1) Keep the raw API functions in features/editor/api/ as plain functions with no React in them, then put the TanStack Query wrappers in a separate queries file next to them. That way the query options are reusable and the API layer stays testable on its own. 2) One Zustand store per feature, with slices inside it if it grows β I wouldn't add a provider per store. A provider only earns its place when you genuinely need per-instance state, like two editors on the same page; otherwise a plain store is simpler and avoids re-render traps. 3) Domain logic (geometry, your document model) goes in features/editor/domain/ as pure functions with no React imports. Utils is only for genuinely generic helpers, and lib/services is for cross-cutting infra like auth or storage. 4) Only move code to src/shared/ when a second feature actually imports it β pulling it out early is how you end up with a shared folder full of things only one feature uses. 5) Treat TanStack Query as the source of truth for server state, and keep Zustand for local UI and editing state only. Fetch with a query, hydrate the editor from it, and push changes back with mutations β duplicating server data into Zustand and trying to keep the two in sync is where most of the pain comes from.
-4
u/Any-Ring6621 14d ago
The AI does it
0
u/Mysterious-Law-3416 15d ago
/api inside editor
keep zustand and providers in stores folder
/utils for util funcs, /schemas for zod schemas
/shared inside /features
depends
-1
u/karmatics29 15d ago
I'd keep three layers apart. Raw API functions live in a plain api file per feature, returning typed data with no React. Query options (queryKey plus queryFn) wrap those, and hooks just call useQuery with them.
A domain/ folder is fine if it holds pure functions with no imports from React, Zustand or fetch: geometry, snapping, document transforms, constraint validation. Those are trivial to unit test. Keep utils/ for generic helpers like formatting an aspect ratio string.
For server versus local state, treat the query result as the baseline and initialize the Zustand session once from it, keyed by document id. Set refetchOnWindowFocus to false for that query, or skip syncing while the store has a dirty flag. Compare a server version or updatedAt on save to catch conflicts. Use slices while they share one undo history and selection model, and split stores only when lifecycles differ.