Commit 1a2d62d
authored
refactor(hooks): isolate localStorage side-effects from state updater (JhaSourav07#973)
## Description
Fixes JhaSourav07#544
This PR refactors the `useRecentSearches` hook to ensure React state
updater callbacks remain completely pure and side-effect free.
Previously, `localStorage` synchronization was executed directly inside
functional state updater callbacks:
```ts
setState((prev) => {
localStorage.setItem(...)
return updatedState
})
```
This violates React concurrent rendering best practices because updater
callbacks are expected to remain deterministic and free of external
side-effects.
Under React Strict Mode and concurrent rendering verification cycles,
updater callbacks may execute multiple times, causing:
- redundant localStorage writes
- duplicate side-effects
- potential state/storage desynchronization
- unnecessary I/O operations
---
## Changes Implemented
### Pure State Updater Refactor
Refactored all updater callbacks inside:
- `addSearch`
- `removeSearch`
- `clearSearches`
to remain completely pure and deterministic.
Removed all direct `writeStorage(...)` calls from updater calculations.
### Reactive localStorage Synchronization
Implemented a dedicated reactive `useEffect` synchronization layer:
```ts
useEffect(() => {
if (!state.mounted) return;
if (state.searches.length === 0) {
writeStorage(null);
} else {
writeStorage(state.searches);
}
}, [state.searches, state.mounted]);
```
This guarantees:
- side-effects execute only after committed renders
- updater callbacks remain concurrency-safe
- Strict Mode double-invocation no longer causes duplicate writes
### Added Regression Test Coverage
Added new regression tests validating:
- React Strict Mode safety
- localStorage synchronization correctness
- side-effect isolation behavior
- deterministic updater execution
---
## Files Changed
- `hooks/useRecentSearches.ts`
- `hooks/useRecentSearches.test.ts`
---
## Root Cause
The previous implementation executed localStorage side-effects directly
inside React functional updater callbacks:
```ts
setState((prev) => {
writeStorage(...)
return updatedState
})
```
React may intentionally invoke updater callbacks multiple times under:
- Strict Mode
- concurrent rendering
- render verification cycles
Because side-effects were embedded inside updater calculations:
- duplicate writes occurred
- updater purity was violated
- synchronization behavior became fragile
---
## Validation
Successfully verified with:
- `npm run test`
- `npm run lint`
- `npm run format`
- `npm run build`
### Results
- 479/479 tests passed
- ESLint passed successfully
- Prettier formatting passed successfully
- Production build compiled successfully
---
## Strict Mode Validation
Verified successfully using:
- `React.StrictMode`
- repeated updater execution scenarios
- localStorage write spies
Confirmed:
- updater callbacks remain deterministic
- localStorage writes occur reactively
- duplicate side-effects no longer occur
- persistence behavior remains correct
---
## Functional Validation
Verified:
- recent searches persist correctly
- ordering remains stable
- deduplication behavior remains intact
- max-history trimming remains unchanged
- storage hydration remains functional
- no UI regressions occur
---
## Regression Safety
Confirmed:
- no hydration issues
- no infinite render loops
- no stale closure bugs
- no unnecessary re-renders
- no TypeScript issues
- no lint violations
---
## Impact
This PR fixes:
- React updater impurity
- duplicate localStorage writes
- Strict Mode side-effect duplication
- concurrent rendering instability
- localStorage synchronization fragility
while preserving:
- existing hook API
- persistence behavior
- ordering guarantees
- backward compatibility
---
## Pillar
- [ ] 🎨 Pillar 1 — New Theme Design
- [ ] 📐 Pillar 2 — Geometric SVG Improvement
- [ ] 🕐 Pillar 3 — Timezone Logic Optimization
- [x] 🛠️ Other (Bug fix / React architecture refactor)
---
## Visual Preview
N/A — React hook architecture and persistence synchronization refactor
---
## Checklist before requesting a review:
- [x] I have read the `CONTRIBUTING.md` file.
- [x] I have tested these changes locally.
- [x] I have run `npm run format` and `npm run lint` locally and
resolved all errors.
- [x] My commits follow the Conventional Commits format.
- [x] I have made sure that I have only one commit in this PR.
- [x] All tests and production builds pass successfully.2 files changed
Lines changed: 127 additions & 59 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
76 | 77 | | |
77 | 78 | | |
78 | 79 | | |
| 80 | + | |
79 | 81 | | |
80 | 82 | | |
81 | 83 | | |
| |||
102 | 104 | | |
103 | 105 | | |
104 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
105 | 148 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
12 | 20 | | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
20 | 26 | | |
21 | 27 | | |
22 | | - | |
23 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
24 | 33 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
34 | 69 | | |
35 | | - | |
36 | 70 | | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
37 | 85 | | |
38 | 86 | | |
39 | | - | |
40 | 87 | | |
41 | 88 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
| 89 | + | |
70 | 90 | | |
71 | 91 | | |
72 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
73 | 96 | | |
74 | 97 | | |
75 | 98 | | |
76 | 99 | | |
77 | 100 | | |
| 101 | + | |
78 | 102 | | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
84 | 108 | | |
85 | 109 | | |
| 110 | + | |
86 | 111 | | |
87 | 112 | | |
88 | 113 | | |
89 | | - | |
90 | 114 | | |
| 115 | + | |
91 | 116 | | |
92 | 117 | | |
0 commit comments