use-good-hooks 1.0.35 โ†’ 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.
Files changed (2) hide show
  1. package/README.md +49 -1
  2. 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
- - `debounceSettings`: Debounce settings object (default: { leading: true, trailing: false })
247
246
  - `debounceMs`: Time in milliseconds to debounce the state changes (default: 0)
247
+ - `debounceSettings`: Debounce settings object (default: { leading: true, trailing: false })
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
@@ -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/package.json CHANGED
@@ -50,5 +50,5 @@
50
50
  "test:coverage": "vitest --coverage --run",
51
51
  "test:watch": "vitest"
52
52
  },
53
- "version": "1.0.35"
53
+ "version": "1.0.36"
54
54
  }