Skip to main content

Component and State Architecture

Good React architecture makes change predictable by aligning ownership, data flow, async boundaries, failure boundaries, and runtime boundaries.

Architecture starts with ownership​

Every state decision starts with ownership
Owner
Who is allowed to define the source of truth?
Readers
Which parts of the tree need the value?
Writers
Which interactions are allowed to change it?
Lifetime
Render, component, route, session, or persistent?
Navigation
Should reload/back/forward preserve it?
Category
Local, URL, server, external, form, or derived?

Local UI state should usually stay local. Lift it only when coordination genuinely requires a common owner.

Lift state only to the lowest common owner
Checkout
Owns selected shipping method
ShippingMethod
Reads + writes
OrderSummary
Reads

State categories are architectural boundaries​

What kind of state is this?
Computed completely from current props/stateDerive during render
Used by one interaction regionLocal component state
Shared by a small subtreeLift state or focused Context
Shareable/bookmarkable/navigation-relevantURL state
Remote data with freshness/retry/invalidationServer-state or framework data layer
Lives outside React and exposes subscriptionsExternal store + useSyncExternalStore-compatible contract

Do not duplicate one fact into several owners merely because different screens need it.

Feature-oriented structure​

Grouping by feature can make ownership visible:

features/
checkout/
components/
hooks/
data/
state/
tests/
search/
components/
hooks/
data/

Literal directory trees are useful here because the structure itself is the subject. The principle matters more than the exact folder names: related code should evolve behind an explicit feature boundary.

Separate domain logic from rendering​

export function calculateOrderTotal(lines, discount) {
return applyDiscount(sumLines(lines), discount);
}

Pure domain rules should not require React to test or reuse them.

Keep domain rules below the UI boundary
React feature
Events · state · loading · rendering
Domain logic
Pure rules · validation · calculations

Component boundaries should have a reason​

Useful responsibilities include:

  • reusable semantic primitive;
  • local interaction owner;
  • feature orchestrator;
  • provider boundary;
  • third-party adapter;
  • async/loading/failure boundary.
Should this become a separate component?
It owns a meaningful interaction or lifecycleStrong boundary
It provides a reusable semantic/API contractStrong boundary
It isolates a failure, async, or vendor boundaryStrong boundary
It only forwards props with no independent conceptProbably keep it together

Avoid both a 2,000-line component and dozens of wrappers with no architectural meaning.

Custom Hooks package behavior, not shared state​

const { draft, updateQuantity, submit } = useCheckoutDraft(orderId);

A custom Hook can reuse a stateful/synchronization process. Separate Hook calls still have separate state unless they subscribe to a shared owner.

Custom Hook reuse does not imply shared ownership
Checkout A
useCheckoutDraft()
Checkout B
useCheckoutDraft()
Shared implementation
same Hook code · independent Hook state unless backed by a shared store

Context distributes dependencies​

Context is useful when a dependency belongs to a subtree and explicit prop threading would obscure the API.

Context distributes access; it does not decide ownership
Actual owner
state · reducer · external store · server/session source
Focused Provider
defines subtree scope
Reader A
uses value
Reader B
uses value
Dispatcher
may only need commands

For reducer architectures, separate state and dispatch Contexts when that clarifies dependencies or update surfaces.

URL and server state have different contracts​

URL state
shareable · bookmarkable · back/forward · reload persistence
Server state
freshness · cache · retries · invalidation · optimistic reconciliation

A search filter that belongs in /search?q=react&page=3 should not exist only as hidden component state. Remote data should not be copied into ad-hoc global client state unless the client truly becomes its owner.

Async, failure, and priority boundaries should match UX​

Different React boundaries answer different product questions
Suspense
What reveals together?
Error Boundary
What may fail independently?
Transition
What work may be non-urgent?

Place these around meaningful user experiences, not arbitrary file boundaries.

Server/client architecture​

Keep the client boundary as small as interaction requires
Server-friendly work
data access · secrets · heavy transformation · non-interactive rendering
Client island
state · events · Effects · browser APIs

A 'use client' boundary is a module-graph decision, not merely a rendering label. Keep server-only dependencies outside the client graph and pass only serializable data needed by the interactive region.

Architecture review loop​

1Identify owner
2Choose state category
3Place component boundary
4Map async/failure UX
5Verify runtime/trust boundary
6Test + observe

A maintainable architecture makes ownership visible, keeps dependencies directional, and lets each boundary change for a clear reason.