Skip to content

react-patterns

Principles for building production-ready React applications.


TypeUseState
ServerData fetching, staticNone
ClientInteractivityuseState, effects
PresentationalUI displayProps only
ContainerLogic/stateHeavy state
  • One responsibility per component
  • Props down, events up
  • Composition over inheritance
  • Prefer small, focused components

PatternExtract When
useLocalStorageSame storage logic needed
useDebounceMultiple debounced values
useFetchRepeated fetch patterns
useFormComplex form state
  • Hooks at top level only
  • Same order every render
  • Custom hooks start with “use”
  • Clean up effects on unmount

ComplexitySolution
SimpleuseState, useReducer
Shared localContext
Server stateReact Query, SWR
Complex globalZustand, Redux Toolkit
ScopeWhere
Single componentuseState
Parent-childLift state up
SubtreeContext
App-wideGlobal store

HookPurpose
useActionStateForm submission state
useOptimisticOptimistic UI updates
useRead resources in render
  • Automatic memoization
  • Less manual useMemo/useCallback
  • Focus on pure components

  • Parent provides context
  • Children consume context
  • Flexible slot-based composition
  • Example: Tabs, Accordion, Dropdown
Use CasePrefer
Reusable logicCustom hook
Render flexibilityRender props
Cross-cuttingHigher-order component

SignalAction
Slow rendersProfile first
Large listsVirtualize
Expensive calcuseMemo
Stable callbacksuseCallback
  1. Check if actually slow
  2. Profile with DevTools
  3. Identify bottleneck
  4. Apply targeted fix

ScopePlacement
App-wideRoot level
FeatureRoute/feature level
ComponentAround risky component
  • Show fallback UI
  • Log error
  • Offer retry option
  • Preserve user data

PatternUse
InterfaceComponent props
TypeUnions, complex
GenericReusable components
NeedType
ChildrenReactNode
Event handlerMouseEventHandler
RefRefObject

LevelFocus
UnitPure functions, hooks
IntegrationComponent behavior
E2EUser flows
  • User-visible behavior
  • Edge cases
  • Error states
  • Accessibility

❌ Don’t✅ Do
Prop drilling deepUse context
Giant componentsSplit smaller
useEffect for everythingServer components
Premature optimizationProfile first
Index as keyStable unique ID

Remember: React is about composition. Build small, combine thoughtfully.

Always identify gaps and suggest next steps to users. In case there is no gaps anymore, then AI should clearly state that there is no gap left.