Choose state ownership and dependency direction
Choose one owner for each state value
State ownership determines which component or store can change a value and which consumers read it. Put local display choices near their consumers, lift a shared filter to their common parent, use the URL for shareable navigation state when appropriate, and let a server-data cache own fetched records when that complexity is justified. Never maintain a second independent copy of the filtered list just to show a count. In this example the route owns the filter and receives records from its parent.
export function selectOrders(orders, status) { if (!['all','open','closed'].includes(status)) throw new RangeError('Unknown status'); return status === 'all' ? orders.slice() : orders.filter(order => order.status === status);}The function returns a new list without changing input records. All returns a shallow array copy, so the record objects remain shared and should be treated as read-only. Invalid status is rejected. With A open and B closed, selecting open yields A. The domain can be tested using Node without a browser.
import React, {useState} from 'react';import {selectOrders} from './filters.mjs';import {OrderSummary} from './OrderSummary.tsx';export function OrdersRoute({orders}) { const [status,setStatus]=useState('all'); const visible=selectOrders(orders,status); return <main><h1>Orders dashboard</h1> <OrderSummary orders={orders}/> <label>Status <select value={status} onChange={event=>setStatus(event.target.value)}> <option value="all">All</option><option value="open">Open</option><option value="closed">Closed</option> </select></label> <p>{visible.length} visible orders</p> {visible.length===0 ? <p>No matching orders</p> : <ul>{visible.map(order=><li key={order.id}>{order.id}: {order.status}</li>)}</ul>} </main>;}The filter controls the list and visible count in one place. The summary deliberately uses all orders, not the filtered subset, and its heading says open orders. Selecting closed displays B and leaves the global open-order summary unchanged. The native select has a visible label; stable IDs preserve list identity. A production route could move the filter into search parameters if back/forward navigation and sharing are requirements. Avoid storing a derived list in state or synchronizing it with an Effect.
Compose dependencies at the application edge
import React, {useEffect,useState} from 'react';import {createMemoryRepository,createOrderService} from './service.mjs';import {OrdersRoute} from './OrdersRoute.tsx';export function Dashboard({service}) { const [result,setResult]=useState({kind:'loading'}); useEffect(()=>{ let active=true; service.list().then(orders=>{if(active)setResult({kind:'ready',orders});},()=>{if(active)setResult({kind:'error'});}); return ()=>{active=false;}; },[service]); if(result.kind==='loading') return <p role="status">Loading orders</p>; if(result.kind==='error') return <p role="alert">Orders unavailable</p>; return <OrdersRoute orders={result.orders}/>;}// Composition root: construct the dependency once outside rendering.const repository=createMemoryRepository([{id:'A',cents:1250,status:'open'},{id:'B',cents:900,status:'closed'}]);const service=createOrderService(repository);export default function App(){return <Dashboard service={service}/>;}The app entry constructs a fixture service once and passes it into Dashboard. The route and summary know nothing about how data was loaded. The Effect loads from the injected service, displays loading or error, and ignores results after cleanup; this is not cancellation. The example uses one stable service per mount. If swapping services at runtime is required, remount with a distinct key or explicitly design result identity and reset behavior so stale account data cannot flash. For real applications prefer an existing route loader or query cache if it handles retries and lifecycle better. Do not construct a fresh service on each render, since the dependency would change repeatedly. Dependency direction describes imports: presentation may depend on domain logic, but domain logic should not depend on presentation. Inject an adapter rather than letting a shared module import its callers. For tests pass an independent fixture per case; test loading, rejection, empty data and unmount separately in React. Practice: explain why two tabs sharing a singleton filter might be undesirable.