@aweebit/react-essentials 0.8.1 → 0.10.0
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 +23 -212
- package/dist/hooks/index.d.ts +0 -1
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +0 -1
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/useEventListener.d.ts +2 -2
- package/dist/hooks/useEventListener.d.ts.map +1 -1
- package/dist/misc/createSafeContext.d.ts +2 -33
- package/dist/misc/createSafeContext.d.ts.map +1 -1
- package/dist/misc/createSafeContext.js +0 -3
- package/dist/misc/createSafeContext.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/index.ts +0 -1
- package/src/hooks/useEventListener.ts +2 -2
- package/src/misc/createSafeContext.ts +6 -37
- package/dist/hooks/useForceUpdate.d.ts +0 -31
- package/dist/hooks/useForceUpdate.d.ts.map +0 -1
- package/dist/hooks/useForceUpdate.js +0 -43
- package/dist/hooks/useForceUpdate.js.map +0 -1
- package/src/hooks/useForceUpdate.ts +0 -47
package/README.md
CHANGED
|
@@ -2,29 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@aweebit/react-essentials)
|
|
4
4
|
|
|
5
|
-
### Requirements
|
|
6
|
-
|
|
7
|
-
- React ≥ 18
|
|
8
|
-
- TypeScript ≥ 5.4
|
|
9
|
-
|
|
10
|
-
### Functions
|
|
11
|
-
|
|
12
5
|
- [useEventListener()](#useeventlistener)
|
|
13
|
-
- [useForceUpdate()](#useforceupdate)
|
|
14
6
|
- [useReducerWithDeps()](#usereducerwithdeps)
|
|
15
7
|
- [useStateWithDeps()](#usestatewithdeps)
|
|
16
8
|
- [createSafeContext()](#createsafecontext)
|
|
17
9
|
|
|
18
|
-
###
|
|
10
|
+
### Requirements
|
|
19
11
|
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
- [UseEventListenerWithExplicitTarget](#useeventlistenerwithexplicittarget)
|
|
23
|
-
- [UseEventListenerWithAnyExplicitTarget](#useeventlistenerwithanyexplicittarget)
|
|
24
|
-
- [UseEventListenerWithImplicitWindowTargetArgs](#useeventlistenerwithimplicitwindowtargetargs)
|
|
25
|
-
- [UseEventListenerWithExplicitTargetArgs](#useeventlistenerwithexplicittargetargs)
|
|
26
|
-
- [RestrictedContext](#restrictedcontext)
|
|
27
|
-
- [SafeContext](#safecontext)
|
|
12
|
+
- React ≥ 18
|
|
13
|
+
- TypeScript ≥ 5.4
|
|
28
14
|
|
|
29
15
|
## useEventListener
|
|
30
16
|
|
|
@@ -32,7 +18,7 @@
|
|
|
32
18
|
const useEventListener: UseEventListener;
|
|
33
19
|
```
|
|
34
20
|
|
|
35
|
-
Defined in: [hooks/useEventListener.ts:135](https://github.com/aweebit/react-essentials/blob/v0.
|
|
21
|
+
Defined in: [hooks/useEventListener.ts:135](https://github.com/aweebit/react-essentials/blob/v0.10.0/src/hooks/useEventListener.ts#L135)
|
|
36
22
|
|
|
37
23
|
Adds `handler` as a listener for the event `eventName` of `target` with the
|
|
38
24
|
provided `options` applied
|
|
@@ -73,78 +59,6 @@ useEventListener(buttonRef, 'click', () => console.log('click'));
|
|
|
73
59
|
|
|
74
60
|
---
|
|
75
61
|
|
|
76
|
-
## ~~useForceUpdate()~~
|
|
77
|
-
|
|
78
|
-
```ts
|
|
79
|
-
function useForceUpdate(callback?): [() => void, bigint];
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
Defined in: [hooks/useForceUpdate.ts:37](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/hooks/useForceUpdate.ts#L37)
|
|
83
|
-
|
|
84
|
-
Enables you to imperatively trigger re-rendering of components
|
|
85
|
-
|
|
86
|
-
This hook is designed in the most general way possible in order to cover all
|
|
87
|
-
imaginable use cases.
|
|
88
|
-
|
|
89
|
-
### Deprecated
|
|
90
|
-
|
|
91
|
-
This hook encourages patterns that are unsafe in Concurrent React.
|
|
92
|
-
For details and ideas on how to get rid of it, please check the discussion at
|
|
93
|
-
https://www.reddit.com/r/react/comments/1nqcsri/comment/ng76cv5/.
|
|
94
|
-
|
|
95
|
-
### Parameters
|
|
96
|
-
|
|
97
|
-
<table>
|
|
98
|
-
<thead>
|
|
99
|
-
<tr>
|
|
100
|
-
<th>Parameter</th>
|
|
101
|
-
<th>Type</th>
|
|
102
|
-
<th>Description</th>
|
|
103
|
-
</tr>
|
|
104
|
-
</thead>
|
|
105
|
-
<tbody>
|
|
106
|
-
<tr>
|
|
107
|
-
<td>
|
|
108
|
-
|
|
109
|
-
`callback?`
|
|
110
|
-
|
|
111
|
-
</td>
|
|
112
|
-
<td>
|
|
113
|
-
|
|
114
|
-
() => `void`
|
|
115
|
-
|
|
116
|
-
</td>
|
|
117
|
-
<td>
|
|
118
|
-
|
|
119
|
-
An optional callback function to call during renders that
|
|
120
|
-
were triggered with `forceUpdate()`
|
|
121
|
-
|
|
122
|
-
Can be used for conditionally calling state setters when state needs to be
|
|
123
|
-
reset. That is legal and better than using effects (see
|
|
124
|
-
[You Might Not Need an Effect \> Adjusting some state when a prop changes](https://react.dev/learn/-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes)),
|
|
125
|
-
but can often be avoided by using [`useStateWithDeps`](#usestatewithdeps) or
|
|
126
|
-
[`useReducerWithDeps`](#usereducerwithdeps).
|
|
127
|
-
|
|
128
|
-
Important: the callback function is called once per render, not once per
|
|
129
|
-
`forceUpdate` call! If React batches `forceUpdate` calls, then it will only
|
|
130
|
-
be called once.
|
|
131
|
-
|
|
132
|
-
</td>
|
|
133
|
-
</tr>
|
|
134
|
-
</tbody>
|
|
135
|
-
</table>
|
|
136
|
-
|
|
137
|
-
### Returns
|
|
138
|
-
|
|
139
|
-
\[() => `void`, `bigint`\]
|
|
140
|
-
|
|
141
|
-
An array with the following two elements:
|
|
142
|
-
|
|
143
|
-
1. A `forceUpdate` function that triggers a re-render
|
|
144
|
-
2. The number of times `forceUpdate` has been called so far
|
|
145
|
-
|
|
146
|
-
---
|
|
147
|
-
|
|
148
62
|
## useReducerWithDeps()
|
|
149
63
|
|
|
150
64
|
```ts
|
|
@@ -155,7 +69,7 @@ function useReducerWithDeps<S, A>(
|
|
|
155
69
|
): [S, ActionDispatch<A>];
|
|
156
70
|
```
|
|
157
71
|
|
|
158
|
-
Defined in: [hooks/useReducerWithDeps.ts:59](https://github.com/aweebit/react-essentials/blob/v0.
|
|
72
|
+
Defined in: [hooks/useReducerWithDeps.ts:59](https://github.com/aweebit/react-essentials/blob/v0.10.0/src/hooks/useReducerWithDeps.ts#L59)
|
|
159
73
|
|
|
160
74
|
`useReducer` hook with an additional dependency array `deps` that resets the
|
|
161
75
|
state to `initialState` when dependencies change
|
|
@@ -298,7 +212,7 @@ function useStateWithDeps<S>(
|
|
|
298
212
|
): [S, Dispatch<SetStateAction<S>>];
|
|
299
213
|
```
|
|
300
214
|
|
|
301
|
-
Defined in: [hooks/useStateWithDeps.ts:62](https://github.com/aweebit/react-essentials/blob/v0.
|
|
215
|
+
Defined in: [hooks/useStateWithDeps.ts:62](https://github.com/aweebit/react-essentials/blob/v0.10.0/src/hooks/useStateWithDeps.ts#L62)
|
|
302
216
|
|
|
303
217
|
`useState` hook with an additional dependency array `deps` that resets the
|
|
304
218
|
state to `initialState` when dependencies change
|
|
@@ -427,10 +341,12 @@ Dependencies that reset the state to `initialState`
|
|
|
427
341
|
```ts
|
|
428
342
|
function createSafeContext<T>(): <DisplayName>(
|
|
429
343
|
displayName,
|
|
430
|
-
) =>
|
|
344
|
+
) => { [K in `${string}Context`]: Context<T> } & {
|
|
345
|
+
[K in `use${string}`]: () => T;
|
|
346
|
+
};
|
|
431
347
|
```
|
|
432
348
|
|
|
433
|
-
Defined in: [misc/createSafeContext.ts:
|
|
349
|
+
Defined in: [misc/createSafeContext.ts:62](https://github.com/aweebit/react-essentials/blob/v0.10.0/src/misc/createSafeContext.ts#L62)
|
|
434
350
|
|
|
435
351
|
For a given type `T`, returns a function that produces both a context of that
|
|
436
352
|
type and a hook that returns the current context value if one was provided,
|
|
@@ -516,7 +432,7 @@ A function that accepts a single string argument `displayName` (e.g.
|
|
|
516
432
|
current context value if one was provided, or throws an error otherwise
|
|
517
433
|
|
|
518
434
|
```ts
|
|
519
|
-
<DisplayName>(displayName):
|
|
435
|
+
<DisplayName>(displayName): { [K in `${string}Context`]: Context<T> } & { [K in `use${string}`]: () => T };
|
|
520
436
|
```
|
|
521
437
|
|
|
522
438
|
#### Type Parameters
|
|
@@ -565,11 +481,7 @@ A function that accepts a single string argument `displayName` (e.g.
|
|
|
565
481
|
|
|
566
482
|
#### Returns
|
|
567
483
|
|
|
568
|
-
[`
|
|
569
|
-
|
|
570
|
-
### See
|
|
571
|
-
|
|
572
|
-
[`SafeContext`](#safecontext)
|
|
484
|
+
``{ [K in `${string}Context`]: Context<T> }`` & ``{ [K in `use${string}`]: () => T }``
|
|
573
485
|
|
|
574
486
|
---
|
|
575
487
|
|
|
@@ -581,7 +493,7 @@ type UseEventListener = UseEventListenerWithImplicitWindowTarget &
|
|
|
581
493
|
UseEventListenerWithAnyExplicitTarget;
|
|
582
494
|
```
|
|
583
495
|
|
|
584
|
-
Defined in: [hooks/useEventListener.ts:12](https://github.com/aweebit/react-essentials/blob/v0.
|
|
496
|
+
Defined in: [hooks/useEventListener.ts:12](https://github.com/aweebit/react-essentials/blob/v0.10.0/src/hooks/useEventListener.ts#L12)
|
|
585
497
|
|
|
586
498
|
The type of [`useEventListener`](#useeventlistener-1)
|
|
587
499
|
|
|
@@ -600,7 +512,7 @@ The type of [`useEventListener`](#useeventlistener-1)
|
|
|
600
512
|
type UseEventListenerWithImplicitWindowTarget = <K>(...args) => void;
|
|
601
513
|
```
|
|
602
514
|
|
|
603
|
-
Defined in: [hooks/useEventListener.ts:21](https://github.com/aweebit/react-essentials/blob/v0.
|
|
515
|
+
Defined in: [hooks/useEventListener.ts:21](https://github.com/aweebit/react-essentials/blob/v0.10.0/src/hooks/useEventListener.ts#L21)
|
|
604
516
|
|
|
605
517
|
### Type Parameters
|
|
606
518
|
|
|
@@ -668,7 +580,7 @@ type UseEventListenerWithExplicitGlobalTarget =
|
|
|
668
580
|
UseEventListenerWithExplicitTarget<MathMLElement, MathMLElementEventMap>;
|
|
669
581
|
```
|
|
670
582
|
|
|
671
|
-
Defined in: [hooks/useEventListener.ts:32](https://github.com/aweebit/react-essentials/blob/v0.
|
|
583
|
+
Defined in: [hooks/useEventListener.ts:32](https://github.com/aweebit/react-essentials/blob/v0.10.0/src/hooks/useEventListener.ts#L32)
|
|
672
584
|
|
|
673
585
|
### See
|
|
674
586
|
|
|
@@ -685,7 +597,7 @@ type UseEventListenerWithExplicitTarget<Target, EventMap> = <T, K>(
|
|
|
685
597
|
) => void;
|
|
686
598
|
```
|
|
687
599
|
|
|
688
|
-
Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-essentials/blob/v0.
|
|
600
|
+
Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-essentials/blob/v0.10.0/src/hooks/useEventListener.ts#L44)
|
|
689
601
|
|
|
690
602
|
### Type Parameters
|
|
691
603
|
|
|
@@ -693,7 +605,6 @@ Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-esse
|
|
|
693
605
|
<thead>
|
|
694
606
|
<tr>
|
|
695
607
|
<th>Type Parameter</th>
|
|
696
|
-
<th>Default type</th>
|
|
697
608
|
</tr>
|
|
698
609
|
</thead>
|
|
699
610
|
<tbody>
|
|
@@ -702,11 +613,6 @@ Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-esse
|
|
|
702
613
|
|
|
703
614
|
`Target` _extends_ `EventTarget`
|
|
704
615
|
|
|
705
|
-
</td>
|
|
706
|
-
<td>
|
|
707
|
-
|
|
708
|
-
‐
|
|
709
|
-
|
|
710
616
|
</td>
|
|
711
617
|
</tr>
|
|
712
618
|
<tr>
|
|
@@ -714,11 +620,6 @@ Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-esse
|
|
|
714
620
|
|
|
715
621
|
`EventMap`
|
|
716
622
|
|
|
717
|
-
</td>
|
|
718
|
-
<td>
|
|
719
|
-
|
|
720
|
-
`Record`\<`string`, `Event`\>
|
|
721
|
-
|
|
722
623
|
</td>
|
|
723
624
|
</tr>
|
|
724
625
|
</tbody>
|
|
@@ -789,11 +690,13 @@ Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-esse
|
|
|
789
690
|
## UseEventListenerWithAnyExplicitTarget
|
|
790
691
|
|
|
791
692
|
```ts
|
|
792
|
-
type UseEventListenerWithAnyExplicitTarget =
|
|
793
|
-
|
|
693
|
+
type UseEventListenerWithAnyExplicitTarget = UseEventListenerWithExplicitTarget<
|
|
694
|
+
EventTarget,
|
|
695
|
+
Record<string, Event>
|
|
696
|
+
>;
|
|
794
697
|
```
|
|
795
698
|
|
|
796
|
-
Defined in: [hooks/useEventListener.ts:56](https://github.com/aweebit/react-essentials/blob/v0.
|
|
699
|
+
Defined in: [hooks/useEventListener.ts:56](https://github.com/aweebit/react-essentials/blob/v0.10.0/src/hooks/useEventListener.ts#L56)
|
|
797
700
|
|
|
798
701
|
### See
|
|
799
702
|
|
|
@@ -814,7 +717,7 @@ type UseEventListenerWithImplicitWindowTargetArgs<K> =
|
|
|
814
717
|
: never;
|
|
815
718
|
```
|
|
816
719
|
|
|
817
|
-
Defined in: [hooks/useEventListener.ts:64](https://github.com/aweebit/react-essentials/blob/v0.
|
|
720
|
+
Defined in: [hooks/useEventListener.ts:64](https://github.com/aweebit/react-essentials/blob/v0.10.0/src/hooks/useEventListener.ts#L64)
|
|
818
721
|
|
|
819
722
|
### Type Parameters
|
|
820
723
|
|
|
@@ -859,7 +762,7 @@ type UseEventListenerWithExplicitTargetArgs<EventMap, T, K> = [
|
|
|
859
762
|
];
|
|
860
763
|
```
|
|
861
764
|
|
|
862
|
-
Defined in: [hooks/useEventListener.ts:78](https://github.com/aweebit/react-essentials/blob/v0.
|
|
765
|
+
Defined in: [hooks/useEventListener.ts:78](https://github.com/aweebit/react-essentials/blob/v0.10.0/src/hooks/useEventListener.ts#L78)
|
|
863
766
|
|
|
864
767
|
### Type Parameters
|
|
865
768
|
|
|
@@ -897,95 +800,3 @@ Defined in: [hooks/useEventListener.ts:78](https://github.com/aweebit/react-esse
|
|
|
897
800
|
### See
|
|
898
801
|
|
|
899
802
|
[`useEventListener`](#useeventlistener-1)
|
|
900
|
-
|
|
901
|
-
---
|
|
902
|
-
|
|
903
|
-
## RestrictedContext
|
|
904
|
-
|
|
905
|
-
```ts
|
|
906
|
-
type RestrictedContext<T> =
|
|
907
|
-
Context<T> extends Provider<T>
|
|
908
|
-
? {
|
|
909
|
-
Provider: Provider<T>;
|
|
910
|
-
displayName: string;
|
|
911
|
-
} & Provider<T>
|
|
912
|
-
: {
|
|
913
|
-
Provider: Provider<T>;
|
|
914
|
-
displayName: string;
|
|
915
|
-
};
|
|
916
|
-
```
|
|
917
|
-
|
|
918
|
-
Defined in: [misc/createSafeContext.ts:18](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/misc/createSafeContext.ts#L18)
|
|
919
|
-
|
|
920
|
-
A React context with a required `displayName` and the obsolete `Consumer`
|
|
921
|
-
property purposefully omitted so that it is impossible to pass the context
|
|
922
|
-
as an argument to `useContext` or `use` (the hook produced with
|
|
923
|
-
[`createSafeContext`](#createsafecontext) should be used instead)
|
|
924
|
-
|
|
925
|
-
### Type Parameters
|
|
926
|
-
|
|
927
|
-
<table>
|
|
928
|
-
<thead>
|
|
929
|
-
<tr>
|
|
930
|
-
<th>Type Parameter</th>
|
|
931
|
-
</tr>
|
|
932
|
-
</thead>
|
|
933
|
-
<tbody>
|
|
934
|
-
<tr>
|
|
935
|
-
<td>
|
|
936
|
-
|
|
937
|
-
`T`
|
|
938
|
-
|
|
939
|
-
</td>
|
|
940
|
-
</tr>
|
|
941
|
-
</tbody>
|
|
942
|
-
</table>
|
|
943
|
-
|
|
944
|
-
### See
|
|
945
|
-
|
|
946
|
-
[`createSafeContext`](#createsafecontext)
|
|
947
|
-
|
|
948
|
-
---
|
|
949
|
-
|
|
950
|
-
## SafeContext
|
|
951
|
-
|
|
952
|
-
```ts
|
|
953
|
-
type SafeContext<DisplayName, T> = {
|
|
954
|
-
[K in `${DisplayName}Context`]: RestrictedContext<T>;
|
|
955
|
-
} & { [K in `use${DisplayName}`]: () => T };
|
|
956
|
-
```
|
|
957
|
-
|
|
958
|
-
Defined in: [misc/createSafeContext.ts:30](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/misc/createSafeContext.ts#L30)
|
|
959
|
-
|
|
960
|
-
The return type of [`createSafeContext`](#createsafecontext)
|
|
961
|
-
|
|
962
|
-
### Type Parameters
|
|
963
|
-
|
|
964
|
-
<table>
|
|
965
|
-
<thead>
|
|
966
|
-
<tr>
|
|
967
|
-
<th>Type Parameter</th>
|
|
968
|
-
</tr>
|
|
969
|
-
</thead>
|
|
970
|
-
<tbody>
|
|
971
|
-
<tr>
|
|
972
|
-
<td>
|
|
973
|
-
|
|
974
|
-
`DisplayName` _extends_ `string`
|
|
975
|
-
|
|
976
|
-
</td>
|
|
977
|
-
</tr>
|
|
978
|
-
<tr>
|
|
979
|
-
<td>
|
|
980
|
-
|
|
981
|
-
`T`
|
|
982
|
-
|
|
983
|
-
</td>
|
|
984
|
-
</tr>
|
|
985
|
-
</tbody>
|
|
986
|
-
</table>
|
|
987
|
-
|
|
988
|
-
### See
|
|
989
|
-
|
|
990
|
-
[`createSafeContext`](#createsafecontext),
|
|
991
|
-
[`RestrictedContext`](#restrictedcontext)
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC"}
|
package/dist/hooks/index.js
CHANGED
package/dist/hooks/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC"}
|
|
@@ -26,13 +26,13 @@ export type UseEventListenerWithExplicitGlobalTarget = UseEventListenerWithExpli
|
|
|
26
26
|
* {@linkcode useEventListener},
|
|
27
27
|
* {@linkcode UseEventListenerWithExplicitTargetArgs}
|
|
28
28
|
*/
|
|
29
|
-
export type UseEventListenerWithExplicitTarget<Target extends EventTarget, EventMap =
|
|
29
|
+
export type UseEventListenerWithExplicitTarget<Target extends EventTarget, EventMap> = <T extends Target, K extends keyof EventMap>(...args: UseEventListenerWithExplicitTargetArgs<EventMap, T, K>) => void;
|
|
30
30
|
/**
|
|
31
31
|
* @see
|
|
32
32
|
* {@linkcode useEventListener},
|
|
33
33
|
* {@linkcode UseEventListenerWithExplicitTarget}
|
|
34
34
|
*/
|
|
35
|
-
export type UseEventListenerWithAnyExplicitTarget = UseEventListenerWithExplicitTarget<EventTarget
|
|
35
|
+
export type UseEventListenerWithAnyExplicitTarget = UseEventListenerWithExplicitTarget<EventTarget, Record<string, Event>>;
|
|
36
36
|
/**
|
|
37
37
|
* @see
|
|
38
38
|
* {@linkcode useEventListener},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useEventListener.d.ts","sourceRoot":"","sources":["../../src/hooks/useEventListener.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAEnE;;;;;;;;GAQG;AACH,MAAM,MAAM,gBAAgB,GAAG,wCAAwC,GACrE,wCAAwC,GACxC,qCAAqC,CAAC;AAExC;;;;GAIG;AACH,MAAM,MAAM,wCAAwC,GAAG,CACrD,CAAC,SAAS,MAAM,cAAc,EAE9B,GAAG,IAAI,EAAE,4CAA4C,CAAC,CAAC,CAAC,KACrD,IAAI,CAAC;AAEV;;;;GAIG;AACH,MAAM,MAAM,wCAAwC,GAClD,kCAAkC,CAAC,MAAM,EAAE,cAAc,CAAC,GACxD,kCAAkC,CAAC,QAAQ,EAAE,gBAAgB,CAAC,GAC9D,kCAAkC,CAAC,WAAW,EAAE,mBAAmB,CAAC,GACpE,kCAAkC,CAAC,UAAU,EAAE,kBAAkB,CAAC,GAClE,kCAAkC,CAAC,aAAa,EAAE,qBAAqB,CAAC,CAAC;AAE7E;;;;GAIG;AACH,MAAM,MAAM,kCAAkC,CAC5C,MAAM,SAAS,WAAW,EAC1B,QAAQ,
|
|
1
|
+
{"version":3,"file":"useEventListener.d.ts","sourceRoot":"","sources":["../../src/hooks/useEventListener.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAEnE;;;;;;;;GAQG;AACH,MAAM,MAAM,gBAAgB,GAAG,wCAAwC,GACrE,wCAAwC,GACxC,qCAAqC,CAAC;AAExC;;;;GAIG;AACH,MAAM,MAAM,wCAAwC,GAAG,CACrD,CAAC,SAAS,MAAM,cAAc,EAE9B,GAAG,IAAI,EAAE,4CAA4C,CAAC,CAAC,CAAC,KACrD,IAAI,CAAC;AAEV;;;;GAIG;AACH,MAAM,MAAM,wCAAwC,GAClD,kCAAkC,CAAC,MAAM,EAAE,cAAc,CAAC,GACxD,kCAAkC,CAAC,QAAQ,EAAE,gBAAgB,CAAC,GAC9D,kCAAkC,CAAC,WAAW,EAAE,mBAAmB,CAAC,GACpE,kCAAkC,CAAC,UAAU,EAAE,kBAAkB,CAAC,GAClE,kCAAkC,CAAC,aAAa,EAAE,qBAAqB,CAAC,CAAC;AAE7E;;;;GAIG;AACH,MAAM,MAAM,kCAAkC,CAC5C,MAAM,SAAS,WAAW,EAC1B,QAAQ,IACN,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,QAAQ,EAC7C,GAAG,IAAI,EAAE,sCAAsC,CAAC,QAAQ,EAAE,CAAC,EAAE,CAAC,CAAC,KAC5D,IAAI,CAAC;AAEV;;;;GAIG;AACH,MAAM,MAAM,qCAAqC,GAC/C,kCAAkC,CAAC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;AAEzE;;;;GAIG;AACH,MAAM,MAAM,4CAA4C,CACtD,CAAC,SAAS,MAAM,cAAc,IAE9B,sCAAsC,CAAC,cAAc,EAAE,MAAM,EAAE,CAAC,CAAC,SAAS;IACxE,OAAO;IACP,GAAG,MAAM,IAAI;CACd,GACG,IAAI,GACJ,KAAK,CAAC;AAEZ;;;GAGG;AACH,MAAM,MAAM,sCAAsC,CAChD,QAAQ,EACR,CAAC,SAAS,WAAW,EACrB,CAAC,SAAS,MAAM,QAAQ,IACtB;IACF,MAAM,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG;QAAE,gBAAgB,CAAC,EAAE,KAAK,CAAA;KAAE,CAAC,GAAG,IAAI;IAChE,SAAS,EAAE,CAAC;IACZ,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,IAAI;IACvD,OAAO,CAAC,EAAE,uBAAuB,GAAG,OAAO,GAAG,SAAS;CACxD,CAAC;AAYF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,eAAO,MAAM,gBAAgB,EAAE,gBAoDV,CAAC"}
|
|
@@ -1,33 +1,5 @@
|
|
|
1
|
-
import { type Context
|
|
1
|
+
import { type Context } from 'react';
|
|
2
2
|
import type { ArgumentFallback } from '../utils.js';
|
|
3
|
-
/**
|
|
4
|
-
* A React context with a required `displayName` and the obsolete `Consumer`
|
|
5
|
-
* property purposefully omitted so that it is impossible to pass the context
|
|
6
|
-
* as an argument to `useContext` or `use` (the hook produced with
|
|
7
|
-
* {@linkcode createSafeContext} should be used instead)
|
|
8
|
-
*
|
|
9
|
-
* @see
|
|
10
|
-
* {@linkcode createSafeContext}
|
|
11
|
-
*/
|
|
12
|
-
export type RestrictedContext<T> = Context<T> extends Provider<T> ? {
|
|
13
|
-
Provider: Provider<T>;
|
|
14
|
-
displayName: string;
|
|
15
|
-
} & Provider<T> : {
|
|
16
|
-
Provider: Provider<T>;
|
|
17
|
-
displayName: string;
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* The return type of {@linkcode createSafeContext}
|
|
21
|
-
*
|
|
22
|
-
* @see
|
|
23
|
-
* {@linkcode createSafeContext},
|
|
24
|
-
* {@linkcode RestrictedContext}
|
|
25
|
-
*/
|
|
26
|
-
export type SafeContext<DisplayName extends string, T> = {
|
|
27
|
-
[K in `${DisplayName}Context`]: RestrictedContext<T>;
|
|
28
|
-
} & {
|
|
29
|
-
[K in `use${DisplayName}`]: () => T;
|
|
30
|
-
};
|
|
31
3
|
/**
|
|
32
4
|
* For a given type `T`, returns a function that produces both a context of that
|
|
33
5
|
* type and a hook that returns the current context value if one was provided,
|
|
@@ -83,9 +55,6 @@ export type SafeContext<DisplayName extends string, T> = {
|
|
|
83
55
|
* - ``` `${displayName}Context` ``` (e.g. `DirectionContext`): the context
|
|
84
56
|
* - ``` `use${displayName}` ``` (e.g. `useDirection`): a hook that returns the
|
|
85
57
|
* current context value if one was provided, or throws an error otherwise
|
|
86
|
-
*
|
|
87
|
-
* @see
|
|
88
|
-
* {@linkcode SafeContext}
|
|
89
58
|
*/
|
|
90
|
-
export declare function createSafeContext<T = never>(): <DisplayName extends string>(displayName: [T] extends [never] ? never : ArgumentFallback<DisplayName, never, string>) =>
|
|
59
|
+
export declare function createSafeContext<T = never>(): <DisplayName extends string>(displayName: [T] extends [never] ? never : ArgumentFallback<DisplayName, never, string>) => { [K in `${DisplayName}Context`]: Context<T>; } & { [K in `use${DisplayName}`]: () => T; };
|
|
91
60
|
//# sourceMappingURL=createSafeContext.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSafeContext.d.ts","sourceRoot":"","sources":["../../src/misc/createSafeContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,
|
|
1
|
+
{"version":3,"file":"createSafeContext.d.ts","sourceRoot":"","sources":["../../src/misc/createSafeContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAA6B,MAAM,OAAO,CAAC;AAChE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAIpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,GAAG,KAAK,MACjC,WAAW,SAAS,MAAM,EAChC,aAAa,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GAC5B,KAAK,GACL,gBAAgB,CAAC,WAAW,EAAE,KAAK,EAAE,MAAM,CAAC,KAC/C,GAAG,CAAC,IAAI,GAAG,WAAW,SAAS,GAAG,OAAO,CAAC,CAAC,CAAC,GAAE,GAAG,GACjD,CAAC,IAAI,MAAM,WAAW,EAAE,GAAG,MAAM,CAAC,GACpC,CAsBF"}
|
|
@@ -55,9 +55,6 @@ const moValueSymbol = Symbol('noValue');
|
|
|
55
55
|
* - ``` `${displayName}Context` ``` (e.g. `DirectionContext`): the context
|
|
56
56
|
* - ``` `use${displayName}` ``` (e.g. `useDirection`): a hook that returns the
|
|
57
57
|
* current context value if one was provided, or throws an error otherwise
|
|
58
|
-
*
|
|
59
|
-
* @see
|
|
60
|
-
* {@linkcode SafeContext}
|
|
61
58
|
*/
|
|
62
59
|
export function createSafeContext() {
|
|
63
60
|
return (displayName) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSafeContext.js","sourceRoot":"","sources":["../../src/misc/createSafeContext.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"createSafeContext.js","sourceRoot":"","sources":["../../src/misc/createSafeContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAGhE,MAAM,aAAa,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,MAAM,UAAU,iBAAiB;IAC/B,OAAO,CACL,WAEgD,EAGhD,EAAE;QACF,MAAM,WAAW,GAAG,GAAG,WAA0B,SAAkB,CAAC;QACpE,MAAM,QAAQ,GAAG,MAAM,WAA0B,EAAW,CAAC;QAE7D,MAAM,OAAO,GAAG,aAAa,CAA2B,aAAa,CAAC,CAAC;QACvE,OAAO,CAAC,WAAW,GAAG,WAAW,CAAC;QAElC,OAAO;YACL,CAAC,WAAW,CAAC,EAAE,OAAqB;YACpC,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE;gBACf,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;gBAClC,IAAI,KAAK,KAAK,aAAa,EAAE,CAAC;oBAC5B,MAAM,IAAI,KAAK,CAAC,MAAM,WAAW,qBAAqB,CAAC,CAAC;gBAC1D,CAAC;gBACD,OAAO,KAAK,CAAC;YACf,CAAC;SAKF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
package/src/hooks/index.ts
CHANGED
|
@@ -43,7 +43,7 @@ export type UseEventListenerWithExplicitGlobalTarget =
|
|
|
43
43
|
*/
|
|
44
44
|
export type UseEventListenerWithExplicitTarget<
|
|
45
45
|
Target extends EventTarget,
|
|
46
|
-
EventMap
|
|
46
|
+
EventMap,
|
|
47
47
|
> = <T extends Target, K extends keyof EventMap>(
|
|
48
48
|
...args: UseEventListenerWithExplicitTargetArgs<EventMap, T, K>
|
|
49
49
|
) => void;
|
|
@@ -54,7 +54,7 @@ export type UseEventListenerWithExplicitTarget<
|
|
|
54
54
|
* {@linkcode UseEventListenerWithExplicitTarget}
|
|
55
55
|
*/
|
|
56
56
|
export type UseEventListenerWithAnyExplicitTarget =
|
|
57
|
-
UseEventListenerWithExplicitTarget<EventTarget
|
|
57
|
+
UseEventListenerWithExplicitTarget<EventTarget, Record<string, Event>>;
|
|
58
58
|
|
|
59
59
|
/**
|
|
60
60
|
* @see
|
|
@@ -1,38 +1,8 @@
|
|
|
1
|
-
import { type Context,
|
|
1
|
+
import { type Context, createContext, useContext } from 'react';
|
|
2
2
|
import type { ArgumentFallback } from '../utils.js';
|
|
3
3
|
|
|
4
4
|
const moValueSymbol = Symbol('noValue');
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
* A React context with a required `displayName` and the obsolete `Consumer`
|
|
8
|
-
* property purposefully omitted so that it is impossible to pass the context
|
|
9
|
-
* as an argument to `useContext` or `use` (the hook produced with
|
|
10
|
-
* {@linkcode createSafeContext} should be used instead)
|
|
11
|
-
*
|
|
12
|
-
* @see
|
|
13
|
-
* {@linkcode createSafeContext}
|
|
14
|
-
*/
|
|
15
|
-
// The type is conditional so that both React 18 and 19 are correctly supported.
|
|
16
|
-
// The code duplication is necessary for the type to be displayed correctly by
|
|
17
|
-
// TypeDoc.
|
|
18
|
-
export type RestrictedContext<T> =
|
|
19
|
-
Context<T> extends Provider<T>
|
|
20
|
-
? { Provider: Provider<T>; displayName: string } & Provider<T>
|
|
21
|
-
: { Provider: Provider<T>; displayName: string };
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* The return type of {@linkcode createSafeContext}
|
|
25
|
-
*
|
|
26
|
-
* @see
|
|
27
|
-
* {@linkcode createSafeContext},
|
|
28
|
-
* {@linkcode RestrictedContext}
|
|
29
|
-
*/
|
|
30
|
-
export type SafeContext<DisplayName extends string, T> = {
|
|
31
|
-
[K in `${DisplayName}Context`]: RestrictedContext<T>;
|
|
32
|
-
} & {
|
|
33
|
-
[K in `use${DisplayName}`]: () => T;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
6
|
/**
|
|
37
7
|
* For a given type `T`, returns a function that produces both a context of that
|
|
38
8
|
* type and a hook that returns the current context value if one was provided,
|
|
@@ -88,16 +58,15 @@ export type SafeContext<DisplayName extends string, T> = {
|
|
|
88
58
|
* - ``` `${displayName}Context` ``` (e.g. `DirectionContext`): the context
|
|
89
59
|
* - ``` `use${displayName}` ``` (e.g. `useDirection`): a hook that returns the
|
|
90
60
|
* current context value if one was provided, or throws an error otherwise
|
|
91
|
-
*
|
|
92
|
-
* @see
|
|
93
|
-
* {@linkcode SafeContext}
|
|
94
61
|
*/
|
|
95
62
|
export function createSafeContext<T = never>() {
|
|
96
63
|
return <DisplayName extends string>(
|
|
97
64
|
displayName: [T] extends [never]
|
|
98
65
|
? never
|
|
99
66
|
: ArgumentFallback<DisplayName, never, string>,
|
|
100
|
-
):
|
|
67
|
+
): { [K in `${DisplayName}Context`]: Context<T> } & {
|
|
68
|
+
[K in `use${DisplayName}`]: () => T;
|
|
69
|
+
} => {
|
|
101
70
|
const contextName = `${displayName as DisplayName}Context` as const;
|
|
102
71
|
const hookName = `use${displayName as DisplayName}` as const;
|
|
103
72
|
|
|
@@ -105,7 +74,7 @@ export function createSafeContext<T = never>() {
|
|
|
105
74
|
Context.displayName = contextName;
|
|
106
75
|
|
|
107
76
|
return {
|
|
108
|
-
[contextName]: Context as
|
|
77
|
+
[contextName]: Context as Context<T>,
|
|
109
78
|
[hookName]: () => {
|
|
110
79
|
const value = useContext(Context);
|
|
111
80
|
if (value === moValueSymbol) {
|
|
@@ -114,7 +83,7 @@ export function createSafeContext<T = never>() {
|
|
|
114
83
|
return value;
|
|
115
84
|
},
|
|
116
85
|
} as {
|
|
117
|
-
[K in typeof contextName]:
|
|
86
|
+
[K in typeof contextName]: Context<T>;
|
|
118
87
|
} & {
|
|
119
88
|
[K in typeof hookName]: () => T;
|
|
120
89
|
};
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Enables you to imperatively trigger re-rendering of components
|
|
3
|
-
*
|
|
4
|
-
* This hook is designed in the most general way possible in order to cover all
|
|
5
|
-
* imaginable use cases.
|
|
6
|
-
*
|
|
7
|
-
* @deprecated
|
|
8
|
-
* This hook encourages patterns that are unsafe in Concurrent React.
|
|
9
|
-
* For details and ideas on how to get rid of it, please check the discussion at
|
|
10
|
-
* https://www.reddit.com/r/react/comments/1nqcsri/comment/ng76cv5/.
|
|
11
|
-
*
|
|
12
|
-
* @param callback An optional callback function to call during renders that
|
|
13
|
-
* were triggered with `forceUpdate()`
|
|
14
|
-
*
|
|
15
|
-
* Can be used for conditionally calling state setters when state needs to be
|
|
16
|
-
* reset. That is legal and better than using effects (see
|
|
17
|
-
* {@link https://react.dev/learn/-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes You Might Not Need an Effect > Adjusting some state when a prop changes}),
|
|
18
|
-
* but can often be avoided by using {@linkcode useStateWithDeps} or
|
|
19
|
-
* {@linkcode useReducerWithDeps}.
|
|
20
|
-
*
|
|
21
|
-
* Important: the callback function is called once per render, not once per
|
|
22
|
-
* `forceUpdate` call! If React batches `forceUpdate` calls, then it will only
|
|
23
|
-
* be called once.
|
|
24
|
-
*
|
|
25
|
-
* @returns An array with the following two elements:
|
|
26
|
-
*
|
|
27
|
-
* 1. A `forceUpdate` function that triggers a re-render
|
|
28
|
-
* 2. The number of times `forceUpdate` has been called so far
|
|
29
|
-
*/
|
|
30
|
-
export declare function useForceUpdate(callback?: () => void): [() => void, bigint];
|
|
31
|
-
//# sourceMappingURL=useForceUpdate.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useForceUpdate.d.ts","sourceRoot":"","sources":["../../src/hooks/useForceUpdate.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,cAAc,CAAC,QAAQ,CAAC,EAAE,MAAM,IAAI,GAAG,CAAC,MAAM,IAAI,EAAE,MAAM,CAAC,CAU1E"}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { useReducer, useRef } from 'react';
|
|
2
|
-
/* eslint-enable */
|
|
3
|
-
/**
|
|
4
|
-
* Enables you to imperatively trigger re-rendering of components
|
|
5
|
-
*
|
|
6
|
-
* This hook is designed in the most general way possible in order to cover all
|
|
7
|
-
* imaginable use cases.
|
|
8
|
-
*
|
|
9
|
-
* @deprecated
|
|
10
|
-
* This hook encourages patterns that are unsafe in Concurrent React.
|
|
11
|
-
* For details and ideas on how to get rid of it, please check the discussion at
|
|
12
|
-
* https://www.reddit.com/r/react/comments/1nqcsri/comment/ng76cv5/.
|
|
13
|
-
*
|
|
14
|
-
* @param callback An optional callback function to call during renders that
|
|
15
|
-
* were triggered with `forceUpdate()`
|
|
16
|
-
*
|
|
17
|
-
* Can be used for conditionally calling state setters when state needs to be
|
|
18
|
-
* reset. That is legal and better than using effects (see
|
|
19
|
-
* {@link https://react.dev/learn/-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes You Might Not Need an Effect > Adjusting some state when a prop changes}),
|
|
20
|
-
* but can often be avoided by using {@linkcode useStateWithDeps} or
|
|
21
|
-
* {@linkcode useReducerWithDeps}.
|
|
22
|
-
*
|
|
23
|
-
* Important: the callback function is called once per render, not once per
|
|
24
|
-
* `forceUpdate` call! If React batches `forceUpdate` calls, then it will only
|
|
25
|
-
* be called once.
|
|
26
|
-
*
|
|
27
|
-
* @returns An array with the following two elements:
|
|
28
|
-
*
|
|
29
|
-
* 1. A `forceUpdate` function that triggers a re-render
|
|
30
|
-
* 2. The number of times `forceUpdate` has been called so far
|
|
31
|
-
*/
|
|
32
|
-
export function useForceUpdate(callback) {
|
|
33
|
-
// It is very unlikely that the number of updates will exceed
|
|
34
|
-
// Number.MAX_SAFE_INTEGER, but not impossible. That is why we use bigints.
|
|
35
|
-
const [updateCount, forceUpdate] = useReducer((prev) => prev + 1n, 0n);
|
|
36
|
-
const updateCountRef = useRef(updateCount);
|
|
37
|
-
if (updateCount !== updateCountRef.current) {
|
|
38
|
-
updateCountRef.current = updateCount;
|
|
39
|
-
callback?.();
|
|
40
|
-
}
|
|
41
|
-
return [forceUpdate, updateCount];
|
|
42
|
-
}
|
|
43
|
-
//# sourceMappingURL=useForceUpdate.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useForceUpdate.js","sourceRoot":"","sources":["../../src/hooks/useForceUpdate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAK3C,mBAAmB;AAEnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,UAAU,cAAc,CAAC,QAAqB;IAClD,6DAA6D;IAC7D,2EAA2E;IAC3E,MAAM,CAAC,WAAW,EAAE,WAAW,CAAC,GAAG,UAAU,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,GAAG,EAAE,EAAE,EAAE,CAAC,CAAC;IACvE,MAAM,cAAc,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;IAC3C,IAAI,WAAW,KAAK,cAAc,CAAC,OAAO,EAAE,CAAC;QAC3C,cAAc,CAAC,OAAO,GAAG,WAAW,CAAC;QACrC,QAAQ,EAAE,EAAE,CAAC;IACf,CAAC;IACD,OAAO,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;AACpC,CAAC"}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { useReducer, useRef } from 'react';
|
|
2
|
-
|
|
3
|
-
/* eslint-disable */
|
|
4
|
-
import type { useStateWithDeps } from './useStateWithDeps.js';
|
|
5
|
-
import type { useReducerWithDeps } from './useReducerWithDeps.js';
|
|
6
|
-
/* eslint-enable */
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Enables you to imperatively trigger re-rendering of components
|
|
10
|
-
*
|
|
11
|
-
* This hook is designed in the most general way possible in order to cover all
|
|
12
|
-
* imaginable use cases.
|
|
13
|
-
*
|
|
14
|
-
* @deprecated
|
|
15
|
-
* This hook encourages patterns that are unsafe in Concurrent React.
|
|
16
|
-
* For details and ideas on how to get rid of it, please check the discussion at
|
|
17
|
-
* https://www.reddit.com/r/react/comments/1nqcsri/comment/ng76cv5/.
|
|
18
|
-
*
|
|
19
|
-
* @param callback An optional callback function to call during renders that
|
|
20
|
-
* were triggered with `forceUpdate()`
|
|
21
|
-
*
|
|
22
|
-
* Can be used for conditionally calling state setters when state needs to be
|
|
23
|
-
* reset. That is legal and better than using effects (see
|
|
24
|
-
* {@link https://react.dev/learn/-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes You Might Not Need an Effect > Adjusting some state when a prop changes}),
|
|
25
|
-
* but can often be avoided by using {@linkcode useStateWithDeps} or
|
|
26
|
-
* {@linkcode useReducerWithDeps}.
|
|
27
|
-
*
|
|
28
|
-
* Important: the callback function is called once per render, not once per
|
|
29
|
-
* `forceUpdate` call! If React batches `forceUpdate` calls, then it will only
|
|
30
|
-
* be called once.
|
|
31
|
-
*
|
|
32
|
-
* @returns An array with the following two elements:
|
|
33
|
-
*
|
|
34
|
-
* 1. A `forceUpdate` function that triggers a re-render
|
|
35
|
-
* 2. The number of times `forceUpdate` has been called so far
|
|
36
|
-
*/
|
|
37
|
-
export function useForceUpdate(callback?: () => void): [() => void, bigint] {
|
|
38
|
-
// It is very unlikely that the number of updates will exceed
|
|
39
|
-
// Number.MAX_SAFE_INTEGER, but not impossible. That is why we use bigints.
|
|
40
|
-
const [updateCount, forceUpdate] = useReducer((prev) => prev + 1n, 0n);
|
|
41
|
-
const updateCountRef = useRef(updateCount);
|
|
42
|
-
if (updateCount !== updateCountRef.current) {
|
|
43
|
-
updateCountRef.current = updateCount;
|
|
44
|
-
callback?.();
|
|
45
|
-
}
|
|
46
|
-
return [forceUpdate, updateCount];
|
|
47
|
-
}
|