use-good-hooks 1.0.34 โ 1.0.36
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 +50 -2
- package/dist/use-debounce.d.ts +1 -1
- package/dist/use-debounce.js +2 -2
- package/dist/use-history-state.d.ts +1 -1
- package/dist/use-history-state.js +8 -8
- package/dist/use-temporary-state.d.ts +1 -1
- package/dist/use-temporary-state.js +4 -4
- package/dist/use-throttle.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -243,8 +243,8 @@ const TextEditor = () => {
|
|
|
243
243
|
|
|
244
244
|
- `initialState`: The initial state value
|
|
245
245
|
- `options`: (Optional) Configuration options:
|
|
246
|
+
- `debounceMs`: Time in milliseconds to debounce the state changes (default: 0)
|
|
246
247
|
- `debounceSettings`: Debounce settings object (default: { leading: true, trailing: false })
|
|
247
|
-
- `debounceTime`: Time in milliseconds to debounce the state changes (default: 0)
|
|
248
248
|
- `immutable`: Boolean indicating if the state should be treated as immutable (default: false)
|
|
249
249
|
- `maxCapacity`: Maximum number of history entries to keep (default: 10)
|
|
250
250
|
- `onChange`: Function to call when the state changes
|
|
@@ -516,7 +516,7 @@ const [state, setState] = useTemporaryState('initial', 1000);
|
|
|
516
516
|
#### Parameters
|
|
517
517
|
|
|
518
518
|
- `initialState`: The initial state value
|
|
519
|
-
- `
|
|
519
|
+
- `ms`: The timeout duration in milliseconds (default: 3000)
|
|
520
520
|
|
|
521
521
|
#### Returns
|
|
522
522
|
|
|
@@ -524,6 +524,53 @@ const [state, setState] = useTemporaryState('initial', 1000);
|
|
|
524
524
|
- `state`: The current state
|
|
525
525
|
- `setState`: Function to update state
|
|
526
526
|
|
|
527
|
+
### `useLateState`
|
|
528
|
+
|
|
529
|
+
Delays the update of a state value until a specified time has passed. This is useful for scenarios where you want to introduce a delay before a state change takes effect, such as showing a loading spinner for a minimum amount of time.
|
|
530
|
+
|
|
531
|
+
```typescript
|
|
532
|
+
import useLateState from 'use-good-hooks/use-late-state';
|
|
533
|
+
|
|
534
|
+
const DelayedComponent = () => {
|
|
535
|
+
const [status, setStatus, cancelUpdate] = useLateState('Waiting...', 2000); // 2-second delay
|
|
536
|
+
|
|
537
|
+
const handleUpdate = () => {
|
|
538
|
+
setStatus('Updated!');
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
const handleImmediateUpdate = () => {
|
|
542
|
+
setStatus('Immediately Updated!', true);
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
const handleCancel = () => {
|
|
546
|
+
const wasCancelled = cancelUpdate();
|
|
547
|
+
if (wasCancelled) {
|
|
548
|
+
alert('Update cancelled!');
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
return (
|
|
553
|
+
<div>
|
|
554
|
+
<p>Status: {status}</p>
|
|
555
|
+
<button onClick={handleUpdate}>Update after 2s</button>
|
|
556
|
+
<button onClick={handleImmediateUpdate}>Update Immediately</button>
|
|
557
|
+
<button onClick={handleCancel}>Cancel Update</button>
|
|
558
|
+
</div>
|
|
559
|
+
);
|
|
560
|
+
};
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
#### Parameters
|
|
564
|
+
|
|
565
|
+
- `initial`: The initial value of the state.
|
|
566
|
+
- `delay`: The delay in milliseconds before the state is updated.
|
|
567
|
+
|
|
568
|
+
#### Returns
|
|
569
|
+
|
|
570
|
+
- `value`: The current state value.
|
|
571
|
+
- `setLate`: A function to update the state after the specified delay. It can also accept a second boolean argument to update the state immediately.
|
|
572
|
+
- `cancel`: A function to cancel a pending state update.
|
|
573
|
+
|
|
527
574
|
## ๐งช Running Tests
|
|
528
575
|
|
|
529
576
|
This library is thoroughly tested with Vitest and React Testing Library. To run the tests:
|
|
@@ -549,6 +596,7 @@ Each hook in this library is designed with performance in mind:
|
|
|
549
596
|
4. `useUrlState` efficiently handles URL synchronization with debouncing
|
|
550
597
|
5. `useGlobalState` and `createGlobalState` provide a way to share state across components with automatic synchronization
|
|
551
598
|
6. `useTemporaryState` allows for temporary state that resets after a timeout
|
|
599
|
+
7. `useLateState` provides a mechanism to delay state updates, which can be useful for managing UI transitions and asynchronous operations.
|
|
552
600
|
|
|
553
601
|
## ๐ ๏ธ Development
|
|
554
602
|
|
package/dist/use-debounce.d.ts
CHANGED
package/dist/use-debounce.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useState as s, useRef as d, useEffect as n } from "react";
|
|
2
2
|
import f from "lodash/debounce";
|
|
3
|
-
const b = 300,
|
|
3
|
+
const b = 300, p = (e, o = b) => {
|
|
4
4
|
const [u, r] = s(e), c = d(
|
|
5
5
|
f((t) => {
|
|
6
6
|
r(t);
|
|
@@ -16,5 +16,5 @@ const b = 300, D = (e, o = b) => {
|
|
|
16
16
|
}, []), u;
|
|
17
17
|
};
|
|
18
18
|
export {
|
|
19
|
-
|
|
19
|
+
p as default
|
|
20
20
|
};
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { useRef as
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { useRef as M, useReducer as T, useCallback as d } from "react";
|
|
2
|
+
import H from "lodash/cloneDeep";
|
|
3
|
+
import J from "lodash/isNil";
|
|
4
4
|
import E from "lodash/size";
|
|
5
5
|
import k from "./use-debounce-fn.js";
|
|
6
6
|
const q = {
|
|
7
7
|
future: [],
|
|
8
8
|
past: [],
|
|
9
9
|
present: null
|
|
10
|
-
}, l = (e, r) => r ? e :
|
|
10
|
+
}, l = (e, r) => r ? e : H(e), v = (e, r, y) => y ? e === r : JSON.stringify(e) === JSON.stringify(r), z = ({
|
|
11
11
|
action: e,
|
|
12
12
|
immutable: r,
|
|
13
13
|
maxCapacity: y,
|
|
@@ -77,7 +77,7 @@ const q = {
|
|
|
77
77
|
}), w;
|
|
78
78
|
}
|
|
79
79
|
let u = [...n];
|
|
80
|
-
p !== null && (u = [...u, l(p, r)]), !
|
|
80
|
+
p !== null && (u = [...u, l(p, r)]), !J(y) && y > 0 && E(u) > y && (u = u.slice(E(u) - y));
|
|
81
81
|
const c = {
|
|
82
82
|
future: [],
|
|
83
83
|
past: u,
|
|
@@ -106,9 +106,9 @@ const q = {
|
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
}, G = (e, r) => {
|
|
109
|
-
const { maxCapacity: y,
|
|
110
|
-
(i,
|
|
111
|
-
action:
|
|
109
|
+
const { maxCapacity: y, debounceMs: t, debounceSettings: f, onChange: o, immutable: n } = r || {}, S = M(e), [p, s] = T(
|
|
110
|
+
(i, F) => z({
|
|
111
|
+
action: F,
|
|
112
112
|
immutable: n,
|
|
113
113
|
maxCapacity: y,
|
|
114
114
|
onChange: o,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Dispatch } from 'react';
|
|
2
2
|
import { SetStateAction } from 'react';
|
|
3
3
|
|
|
4
|
-
declare const useTemporaryState: <T>(initialState: T,
|
|
4
|
+
declare const useTemporaryState: <T>(initialState: T, ms: number) => readonly [T, Dispatch<SetStateAction<T>>];
|
|
5
5
|
export default useTemporaryState;
|
|
6
6
|
|
|
7
7
|
export { }
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { useRef as
|
|
1
|
+
import { useRef as s, useState as c, useEffect as n } from "react";
|
|
2
2
|
const m = (e, r) => {
|
|
3
|
-
const t =
|
|
3
|
+
const t = s(null), [u, o] = c(e);
|
|
4
4
|
return n(() => (clearTimeout(t.current), t.current = setTimeout(() => {
|
|
5
|
-
|
|
5
|
+
o(e);
|
|
6
6
|
}, r), () => {
|
|
7
7
|
clearTimeout(t.current);
|
|
8
|
-
}), [e, u, r]), [u,
|
|
8
|
+
}), [e, u, r]), [u, o];
|
|
9
9
|
};
|
|
10
10
|
export {
|
|
11
11
|
m as default
|
package/dist/use-throttle.d.ts
CHANGED
package/package.json
CHANGED