Skip to documentation

Design system / Sources & standards

Current repository source

ADR-0032-client-behavior-and-state-escalation.md

docs/decisions/ADR-0032-client-behavior-and-state-escalation.md

This view contains the escaped source read during the deployment build. Code and policy text are unchanged. Markdown is shown as source, not rendered as a document. A new deployment refreshes the content.

# ADR-0032: Client behavior and state escalation

## Status

accepted

## Date

2026-07-14

## Context

The application has interactive forms, uploads, tables, and management screens. Local state is appropriate for much of this work, but it becomes difficult to reason about when request lifecycle, effects, and cross-tree state are placed ad hoc in route components.

The project needs a predictable escalation path that preserves server-first Next.js boundaries and avoids adopting a global client store before an actual shared-state problem exists.

## Options considered

1. **Context-first client state**
   - Use React Context as the normal feature store.
   - Pros: no new dependency.
   - Cons: hides ownership, encourages broad providers, and is a poor default for server data.

2. **Standardize a client store immediately**
   - Select a state-management library now.
   - Pros: one consistent tool.
   - Cons: adds global-state machinery before a demonstrated need.

3. **Server-first, local-state escalation (chosen)**
   - Use URL state, server boundaries, local state, behavior hooks, narrow Context, then a client store only when evidence requires it.
   - Pros: keeps state ownership local and makes escalation intentional.
   - Cons: requires contributors to classify state before adding it.

## Decision

Apply the following state hierarchy in order:

1. Use the URL for shareable, bookmarkable, or back/forward navigation state.
2. Use a server route/layout/component, including a server-side call through an internal API contract where appropriate, for authorized initial server data.
3. Use local component state for simple view state, including one focused input, an open/closed state, selected tab, or other state owned by one component.
4. Create a colocated `use...` behavior hook from the first implementation when a client component has an external subscription or effect, an asynchronous request or mutation lifecycle, or a coordinated multi-step interaction. Hooks return state and commands, not rendered JSX. They may be feature-local and do not need two consumers first.
5. Use a local reducer when one component or behavior hook has related transitions that are clearer as events than independent setters.
6. Use Context only for a stable, ambient contract consumed by many descendants of one bounded subtree, such as compound-component coordination, theme state, or sidebar state. Do not use Context for server data, as a general feature store, or to avoid passing props through one or two levels.
7. Introduce a client-state library only when a single client state domain is read and updated by disconnected branches or routes and needs coordination such as persistence, undo, or cross-surface optimistic updates. Select the library in a separate ADR at that time.

Route and server boundaries retain authorization and initial data loading. A client feature, instance, or its behavior hook may invoke an established server contract through an injected callback or API client abstraction, but components and instances do not embed raw endpoint paths or transport policy.

## Rationale

The hierarchy matches Next.js App Router's server-first model and keeps simple interactions simple. A behavior hook provides an early, explicit seam for effects and state machines without turning every boolean into a custom hook. Context remains a compositional tool, not an unbounded store.

## Consequences

- Interactive route-local code should move request lifecycle and coordinated behavior into colocated hooks when modified.
- Client components can remain small and presentation-led even when their product instances are stateful.
- No client-state package is introduced by this decision.
- A future proposal for a global or external store requires evidence of a qualifying state domain and a separate ADR.

## References

- ADR-0004 - Next.js App Router topology
- ADR-0005 - Rendering strategy by route boundary
- ADR-0031 - Frontend component taxonomy and product instances
All sources and standards
Current components and theme tokens. Fictional example data. Source views refresh on deployment; authors maintain contract notes.