use-good-hooks 1.0.23 → 1.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +129 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -66,6 +66,45 @@ const SearchComponent = () => {
|
|
|
66
66
|
|
|
67
67
|
- The debounced value
|
|
68
68
|
|
|
69
|
+
### `useDebounceFn`
|
|
70
|
+
|
|
71
|
+
Creates a debounced version of a function. This hook ensures that a function is only executed after a specified period of inactivity, preventing it from being called too frequently. It's ideal for handling events like button clicks or API triggers that should not fire on every user action.
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { useDebounceFn } from 'use-good-hooks/use-debounce-fn';
|
|
75
|
+
|
|
76
|
+
const SaveButton = () => {
|
|
77
|
+
const [status, setStatus] = useState('Idle');
|
|
78
|
+
|
|
79
|
+
const debouncedSave = useDebounceFn(() => {
|
|
80
|
+
setStatus('Saving...');
|
|
81
|
+
// Simulate API call
|
|
82
|
+
setTimeout(() => setStatus('Saved!'), 1000);
|
|
83
|
+
}, 1000); // 1000ms delay
|
|
84
|
+
|
|
85
|
+
const handleClick = () => {
|
|
86
|
+
setStatus('Waiting...');
|
|
87
|
+
debouncedSave();
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<div>
|
|
92
|
+
<button onClick={handleClick}>Save Changes</button>
|
|
93
|
+
<p>Status: {status}</p>
|
|
94
|
+
</div>
|
|
95
|
+
);
|
|
96
|
+
};
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### Parameters
|
|
100
|
+
|
|
101
|
+
- `fn`: The function to debounce
|
|
102
|
+
- `delay`: (Optional) The delay in milliseconds (default: 300ms)
|
|
103
|
+
|
|
104
|
+
#### Returns
|
|
105
|
+
|
|
106
|
+
- The debounced function.
|
|
107
|
+
|
|
69
108
|
### `useThrottle`
|
|
70
109
|
|
|
71
110
|
Limits the rate at which a value can update. Useful for scroll events, window resizing, and other high-frequency events.
|
|
@@ -99,6 +138,42 @@ const ScrollTracker = () => {
|
|
|
99
138
|
|
|
100
139
|
- The throttled value
|
|
101
140
|
|
|
141
|
+
### `useThrottleFn`
|
|
142
|
+
|
|
143
|
+
Creates a throttled version of a function, limiting its execution to at most once per specified interval. It is useful for performance-critical scenarios like handling mouse movements, scrolling, or window resizing events without overwhelming the browser.
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
import { useThrottleFn } from 'use-good-hooks/use-throttle-fn';
|
|
147
|
+
|
|
148
|
+
const MouseTracker = () => {
|
|
149
|
+
const [position, setPosition] = useState({ x: 0, y: 0 });
|
|
150
|
+
|
|
151
|
+
const throttledMouseMove = useThrottleFn((event) => {
|
|
152
|
+
setPosition({ x: event.clientX, y: event.clientY });
|
|
153
|
+
}, 300); // Update at most every 300ms
|
|
154
|
+
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
window.addEventListener('mousemove', throttledMouseMove);
|
|
157
|
+
return () => window.removeEventListener('mousemove', throttledMouseMove);
|
|
158
|
+
}, [throttledMouseMove]);
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<div>
|
|
162
|
+
Throttled mouse position: X: {position.x}, Y: {position.y}
|
|
163
|
+
</div>
|
|
164
|
+
);
|
|
165
|
+
};
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### Parameters
|
|
169
|
+
|
|
170
|
+
- `fn`: The function to throttle
|
|
171
|
+
- `delay`: (Optional) The throttle interval in milliseconds (default: 300ms)
|
|
172
|
+
|
|
173
|
+
#### Returns
|
|
174
|
+
|
|
175
|
+
- The throttled function.
|
|
176
|
+
|
|
102
177
|
### `usePrev`
|
|
103
178
|
|
|
104
179
|
Captures the previous value of a state or prop. Useful for comparing changes between renders.
|
|
@@ -127,6 +202,60 @@ const Counter = ({ count }) => {
|
|
|
127
202
|
|
|
128
203
|
- The previous value (undefined on first render)
|
|
129
204
|
|
|
205
|
+
### `useStateHistory`
|
|
206
|
+
|
|
207
|
+
Tracks the history of a state value, providing undo and redo capabilities. This is perfect for building editors, forms, or any UI where users might want to reverse their actions.
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
import { useStateHistory } from 'use-good-hooks/use-state-history';
|
|
211
|
+
|
|
212
|
+
const TextEditor = () => {
|
|
213
|
+
const {
|
|
214
|
+
state,
|
|
215
|
+
setState,
|
|
216
|
+
history,
|
|
217
|
+
back,
|
|
218
|
+
forward,
|
|
219
|
+
canBack,
|
|
220
|
+
canForward,
|
|
221
|
+
} = useStateHistory('', { capacity: 10 });
|
|
222
|
+
|
|
223
|
+
return (
|
|
224
|
+
<div>
|
|
225
|
+
<textarea
|
|
226
|
+
value={state}
|
|
227
|
+
onChange={(e) => setState(e.target.value)}
|
|
228
|
+
rows={4}
|
|
229
|
+
cols={50}
|
|
230
|
+
/>
|
|
231
|
+
<div>
|
|
232
|
+
<button onClick={back} disabled={!canBack}>Undo</button>
|
|
233
|
+
<button onClick={forward} disabled={!canForward}>Redo</button>
|
|
234
|
+
</div>
|
|
235
|
+
<p>History (last {history.length} changes):</p>
|
|
236
|
+
<pre>{JSON.stringify(history, null, 2)}</pre>
|
|
237
|
+
</div>
|
|
238
|
+
);
|
|
239
|
+
};
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
#### Parameters
|
|
243
|
+
|
|
244
|
+
- `initialState`: The initial state value
|
|
245
|
+
- `options`: (Optional) Configuration options:
|
|
246
|
+
- `capacity`: Maximum number of history entries to keep (default: 10)
|
|
247
|
+
|
|
248
|
+
#### Returns
|
|
249
|
+
|
|
250
|
+
- Object with:
|
|
251
|
+
- `state`: The current state value
|
|
252
|
+
- `setState`: Function to update the state and record history
|
|
253
|
+
- `history`: Array of all recorded states
|
|
254
|
+
- `back`: Function to move to the previous state (undo)
|
|
255
|
+
- `forward`: Function to move to the next state (redo)
|
|
256
|
+
- `canBack`: Boolean indicating if undo is possible
|
|
257
|
+
- `canForward`: Boolean indicating if redo is possible
|
|
258
|
+
|
|
130
259
|
### `useDistinct`
|
|
131
260
|
|
|
132
261
|
Detects distinct changes in values with support for deep comparison and custom equality checks. Useful for tracking whether complex objects have actually changed.
|
package/package.json
CHANGED