You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### [CLEAN-REACT-PATTERNS-5] Keep state and subscriptions narrow
1193
+
1194
+
-**Search patterns**: Contexts/hooks/stores exposing large bundled objects, providers with many unrelated `useOnyx` calls, state structures mixing unrelated concerns
1195
+
1196
+
-**Condition**: Flag when a state structure (context, hook, store, or subscription) bundles unrelated concerns together, causing consumers to re-render when data they don't use changes.
1197
+
1198
+
**Signs of violation:**
1199
+
- State provider (context, hook, or store) that bundles unrelated data (e.g., navigation state + list data + cache utilities in one structure)
1200
+
- State object where properties serve different purposes and change independently
1201
+
- Multiple unrelated subscriptions (`useOnyx`, `useContext`, store selectors) aggregated into a single exposed value
1202
+
- Consumers of a state source that only use a subset of the provided values
1203
+
1204
+
**DO NOT flag if:**
1205
+
- State values are cohesive — they change together and serve the same purpose (e.g., `keyboardHeight` + `isKeyboardShown` both relate to keyboard state)
1206
+
- The state structure is intentionally designed as an aggregation point and consumers use most/all values
1207
+
- Individual `useOnyx` calls without selectors — this is covered by [PERF-11]
1208
+
1209
+
-**Reasoning**: When unrelated pieces of data are grouped into a single state structure, if an unused part changes, then all consumers re-render unnecessarily. This silently expands render scope, increases coupling, and makes performance regressions hard to detect. Structuring state around cohesive concerns ensures render scope stays predictable and changes remain local.
1210
+
1211
+
**Distinction from PERF-11**: PERF-11 addresses individual `useOnyx` selector usage. This rule addresses state structure — how multiple values are grouped and exposed to consumers via contexts, hooks, or stores.
1212
+
1213
+
**Distinction from CLEAN-REACT-PATTERNS-2**: PATTERNS-2 addresses data flow direction — parent shouldn't fetch data just to pass to children. This rule addresses how state is structured and grouped within any state provider.
1214
+
1215
+
Good (cohesive state — all values serve one purpose):
1216
+
1217
+
- All state relates to one concern (keyboard)
1218
+
- Values change together — no wasted re-renders
1219
+
- Derived state computed inline, not stored separately
1220
+
1221
+
```tsx
1222
+
typeKeyboardStateContextValue= {
1223
+
isKeyboardShown:boolean;
1224
+
isKeyboardActive:boolean;
1225
+
keyboardHeight:number;
1226
+
};
1227
+
1228
+
function KeyboardStateProvider({children}:ChildrenProps) {
0 commit comments