@aweebit/react-essentials 0.8.1 → 0.9.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 +47 -135
- 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 +12 -12
- package/dist/misc/createSafeContext.d.ts.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 +13 -13
- 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
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
### Functions
|
|
11
11
|
|
|
12
12
|
- [useEventListener()](#useeventlistener)
|
|
13
|
-
- [useForceUpdate()](#useforceupdate)
|
|
14
13
|
- [useReducerWithDeps()](#usereducerwithdeps)
|
|
15
14
|
- [useStateWithDeps()](#usestatewithdeps)
|
|
16
15
|
- [createSafeContext()](#createsafecontext)
|
|
@@ -18,12 +17,6 @@
|
|
|
18
17
|
### Types
|
|
19
18
|
|
|
20
19
|
- [UseEventListener](#useeventlistener-1)
|
|
21
|
-
- [UseEventListenerWithImplicitWindowTarget](#useeventlistenerwithimplicitwindowtarget)
|
|
22
|
-
- [UseEventListenerWithExplicitTarget](#useeventlistenerwithexplicittarget)
|
|
23
|
-
- [UseEventListenerWithAnyExplicitTarget](#useeventlistenerwithanyexplicittarget)
|
|
24
|
-
- [UseEventListenerWithImplicitWindowTargetArgs](#useeventlistenerwithimplicitwindowtargetargs)
|
|
25
|
-
- [UseEventListenerWithExplicitTargetArgs](#useeventlistenerwithexplicittargetargs)
|
|
26
|
-
- [RestrictedContext](#restrictedcontext)
|
|
27
20
|
- [SafeContext](#safecontext)
|
|
28
21
|
|
|
29
22
|
## useEventListener
|
|
@@ -32,7 +25,7 @@
|
|
|
32
25
|
const useEventListener: UseEventListener;
|
|
33
26
|
```
|
|
34
27
|
|
|
35
|
-
Defined in: [hooks/useEventListener.ts:135](https://github.com/aweebit/react-essentials/blob/v0.
|
|
28
|
+
Defined in: [hooks/useEventListener.ts:135](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L135)
|
|
36
29
|
|
|
37
30
|
Adds `handler` as a listener for the event `eventName` of `target` with the
|
|
38
31
|
provided `options` applied
|
|
@@ -73,78 +66,6 @@ useEventListener(buttonRef, 'click', () => console.log('click'));
|
|
|
73
66
|
|
|
74
67
|
---
|
|
75
68
|
|
|
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
69
|
## useReducerWithDeps()
|
|
149
70
|
|
|
150
71
|
```ts
|
|
@@ -155,7 +76,7 @@ function useReducerWithDeps<S, A>(
|
|
|
155
76
|
): [S, ActionDispatch<A>];
|
|
156
77
|
```
|
|
157
78
|
|
|
158
|
-
Defined in: [hooks/useReducerWithDeps.ts:59](https://github.com/aweebit/react-essentials/blob/v0.
|
|
79
|
+
Defined in: [hooks/useReducerWithDeps.ts:59](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useReducerWithDeps.ts#L59)
|
|
159
80
|
|
|
160
81
|
`useReducer` hook with an additional dependency array `deps` that resets the
|
|
161
82
|
state to `initialState` when dependencies change
|
|
@@ -298,7 +219,7 @@ function useStateWithDeps<S>(
|
|
|
298
219
|
): [S, Dispatch<SetStateAction<S>>];
|
|
299
220
|
```
|
|
300
221
|
|
|
301
|
-
Defined in: [hooks/useStateWithDeps.ts:62](https://github.com/aweebit/react-essentials/blob/v0.
|
|
222
|
+
Defined in: [hooks/useStateWithDeps.ts:62](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useStateWithDeps.ts#L62)
|
|
302
223
|
|
|
303
224
|
`useState` hook with an additional dependency array `deps` that resets the
|
|
304
225
|
state to `initialState` when dependencies change
|
|
@@ -430,7 +351,7 @@ function createSafeContext<T>(): <DisplayName>(
|
|
|
430
351
|
) => SafeContext<DisplayName, T>;
|
|
431
352
|
```
|
|
432
353
|
|
|
433
|
-
Defined in: [misc/createSafeContext.ts:95](https://github.com/aweebit/react-essentials/blob/v0.
|
|
354
|
+
Defined in: [misc/createSafeContext.ts:95](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/misc/createSafeContext.ts#L95)
|
|
434
355
|
|
|
435
356
|
For a given type `T`, returns a function that produces both a context of that
|
|
436
357
|
type and a hook that returns the current context value if one was provided,
|
|
@@ -581,7 +502,7 @@ type UseEventListener = UseEventListenerWithImplicitWindowTarget &
|
|
|
581
502
|
UseEventListenerWithAnyExplicitTarget;
|
|
582
503
|
```
|
|
583
504
|
|
|
584
|
-
Defined in: [hooks/useEventListener.ts:12](https://github.com/aweebit/react-essentials/blob/v0.
|
|
505
|
+
Defined in: [hooks/useEventListener.ts:12](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L12)
|
|
585
506
|
|
|
586
507
|
The type of [`useEventListener`](#useeventlistener-1)
|
|
587
508
|
|
|
@@ -600,7 +521,7 @@ The type of [`useEventListener`](#useeventlistener-1)
|
|
|
600
521
|
type UseEventListenerWithImplicitWindowTarget = <K>(...args) => void;
|
|
601
522
|
```
|
|
602
523
|
|
|
603
|
-
Defined in: [hooks/useEventListener.ts:21](https://github.com/aweebit/react-essentials/blob/v0.
|
|
524
|
+
Defined in: [hooks/useEventListener.ts:21](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L21)
|
|
604
525
|
|
|
605
526
|
### Type Parameters
|
|
606
527
|
|
|
@@ -668,7 +589,7 @@ type UseEventListenerWithExplicitGlobalTarget =
|
|
|
668
589
|
UseEventListenerWithExplicitTarget<MathMLElement, MathMLElementEventMap>;
|
|
669
590
|
```
|
|
670
591
|
|
|
671
|
-
Defined in: [hooks/useEventListener.ts:32](https://github.com/aweebit/react-essentials/blob/v0.
|
|
592
|
+
Defined in: [hooks/useEventListener.ts:32](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L32)
|
|
672
593
|
|
|
673
594
|
### See
|
|
674
595
|
|
|
@@ -685,7 +606,7 @@ type UseEventListenerWithExplicitTarget<Target, EventMap> = <T, K>(
|
|
|
685
606
|
) => void;
|
|
686
607
|
```
|
|
687
608
|
|
|
688
|
-
Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-essentials/blob/v0.
|
|
609
|
+
Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L44)
|
|
689
610
|
|
|
690
611
|
### Type Parameters
|
|
691
612
|
|
|
@@ -693,7 +614,6 @@ Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-esse
|
|
|
693
614
|
<thead>
|
|
694
615
|
<tr>
|
|
695
616
|
<th>Type Parameter</th>
|
|
696
|
-
<th>Default type</th>
|
|
697
617
|
</tr>
|
|
698
618
|
</thead>
|
|
699
619
|
<tbody>
|
|
@@ -702,11 +622,6 @@ Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-esse
|
|
|
702
622
|
|
|
703
623
|
`Target` _extends_ `EventTarget`
|
|
704
624
|
|
|
705
|
-
</td>
|
|
706
|
-
<td>
|
|
707
|
-
|
|
708
|
-
‐
|
|
709
|
-
|
|
710
625
|
</td>
|
|
711
626
|
</tr>
|
|
712
627
|
<tr>
|
|
@@ -714,11 +629,6 @@ Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-esse
|
|
|
714
629
|
|
|
715
630
|
`EventMap`
|
|
716
631
|
|
|
717
|
-
</td>
|
|
718
|
-
<td>
|
|
719
|
-
|
|
720
|
-
`Record`\<`string`, `Event`\>
|
|
721
|
-
|
|
722
632
|
</td>
|
|
723
633
|
</tr>
|
|
724
634
|
</tbody>
|
|
@@ -789,11 +699,13 @@ Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-esse
|
|
|
789
699
|
## UseEventListenerWithAnyExplicitTarget
|
|
790
700
|
|
|
791
701
|
```ts
|
|
792
|
-
type UseEventListenerWithAnyExplicitTarget =
|
|
793
|
-
|
|
702
|
+
type UseEventListenerWithAnyExplicitTarget = UseEventListenerWithExplicitTarget<
|
|
703
|
+
EventTarget,
|
|
704
|
+
Record<string, Event>
|
|
705
|
+
>;
|
|
794
706
|
```
|
|
795
707
|
|
|
796
|
-
Defined in: [hooks/useEventListener.ts:56](https://github.com/aweebit/react-essentials/blob/v0.
|
|
708
|
+
Defined in: [hooks/useEventListener.ts:56](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L56)
|
|
797
709
|
|
|
798
710
|
### See
|
|
799
711
|
|
|
@@ -814,7 +726,7 @@ type UseEventListenerWithImplicitWindowTargetArgs<K> =
|
|
|
814
726
|
: never;
|
|
815
727
|
```
|
|
816
728
|
|
|
817
|
-
Defined in: [hooks/useEventListener.ts:64](https://github.com/aweebit/react-essentials/blob/v0.
|
|
729
|
+
Defined in: [hooks/useEventListener.ts:64](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L64)
|
|
818
730
|
|
|
819
731
|
### Type Parameters
|
|
820
732
|
|
|
@@ -859,7 +771,7 @@ type UseEventListenerWithExplicitTargetArgs<EventMap, T, K> = [
|
|
|
859
771
|
];
|
|
860
772
|
```
|
|
861
773
|
|
|
862
|
-
Defined in: [hooks/useEventListener.ts:78](https://github.com/aweebit/react-essentials/blob/v0.
|
|
774
|
+
Defined in: [hooks/useEventListener.ts:78](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L78)
|
|
863
775
|
|
|
864
776
|
### Type Parameters
|
|
865
777
|
|
|
@@ -900,27 +812,17 @@ Defined in: [hooks/useEventListener.ts:78](https://github.com/aweebit/react-esse
|
|
|
900
812
|
|
|
901
813
|
---
|
|
902
814
|
|
|
903
|
-
##
|
|
815
|
+
## SafeContext
|
|
904
816
|
|
|
905
817
|
```ts
|
|
906
|
-
type
|
|
907
|
-
Context
|
|
908
|
-
|
|
909
|
-
Provider: Provider<T>;
|
|
910
|
-
displayName: string;
|
|
911
|
-
} & Provider<T>
|
|
912
|
-
: {
|
|
913
|
-
Provider: Provider<T>;
|
|
914
|
-
displayName: string;
|
|
915
|
-
};
|
|
818
|
+
type SafeContext<DisplayName, T> = {
|
|
819
|
+
[K in `${DisplayName}Context`]: RestrictedContext<T>;
|
|
820
|
+
} & { [K in `use${DisplayName}`]: () => T };
|
|
916
821
|
```
|
|
917
822
|
|
|
918
|
-
Defined in: [misc/createSafeContext.ts:
|
|
823
|
+
Defined in: [misc/createSafeContext.ts:13](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/misc/createSafeContext.ts#L13)
|
|
919
824
|
|
|
920
|
-
|
|
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)
|
|
825
|
+
The return type of [`createSafeContext`](#createsafecontext)
|
|
924
826
|
|
|
925
827
|
### Type Parameters
|
|
926
828
|
|
|
@@ -934,6 +836,13 @@ as an argument to `useContext` or `use` (the hook produced with
|
|
|
934
836
|
<tr>
|
|
935
837
|
<td>
|
|
936
838
|
|
|
839
|
+
`DisplayName` _extends_ `string`
|
|
840
|
+
|
|
841
|
+
</td>
|
|
842
|
+
</tr>
|
|
843
|
+
<tr>
|
|
844
|
+
<td>
|
|
845
|
+
|
|
937
846
|
`T`
|
|
938
847
|
|
|
939
848
|
</td>
|
|
@@ -943,21 +852,32 @@ as an argument to `useContext` or `use` (the hook produced with
|
|
|
943
852
|
|
|
944
853
|
### See
|
|
945
854
|
|
|
946
|
-
[`createSafeContext`](#createsafecontext)
|
|
855
|
+
[`createSafeContext`](#createsafecontext),
|
|
856
|
+
[`RestrictedContext`](#restrictedcontext)
|
|
947
857
|
|
|
948
858
|
---
|
|
949
859
|
|
|
950
|
-
##
|
|
860
|
+
## RestrictedContext
|
|
951
861
|
|
|
952
862
|
```ts
|
|
953
|
-
type
|
|
954
|
-
|
|
955
|
-
|
|
863
|
+
type RestrictedContext<T> =
|
|
864
|
+
Context<T> extends Provider<T>
|
|
865
|
+
? {
|
|
866
|
+
Provider: Provider<T>;
|
|
867
|
+
displayName: string;
|
|
868
|
+
} & Provider<T>
|
|
869
|
+
: {
|
|
870
|
+
Provider: Provider<T>;
|
|
871
|
+
displayName: string;
|
|
872
|
+
};
|
|
956
873
|
```
|
|
957
874
|
|
|
958
|
-
Defined in: [misc/createSafeContext.ts:
|
|
875
|
+
Defined in: [misc/createSafeContext.ts:31](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/misc/createSafeContext.ts#L31)
|
|
959
876
|
|
|
960
|
-
|
|
877
|
+
A React context with a required `displayName` and the obsolete `Consumer`
|
|
878
|
+
property purposefully omitted so that it is impossible to pass the context
|
|
879
|
+
as an argument to `useContext` or `use` (the hook produced with
|
|
880
|
+
[`createSafeContext`](#createsafecontext) should be used instead)
|
|
961
881
|
|
|
962
882
|
### Type Parameters
|
|
963
883
|
|
|
@@ -971,13 +891,6 @@ The return type of [`createSafeContext`](#createsafecontext)
|
|
|
971
891
|
<tr>
|
|
972
892
|
<td>
|
|
973
893
|
|
|
974
|
-
`DisplayName` _extends_ `string`
|
|
975
|
-
|
|
976
|
-
</td>
|
|
977
|
-
</tr>
|
|
978
|
-
<tr>
|
|
979
|
-
<td>
|
|
980
|
-
|
|
981
894
|
`T`
|
|
982
895
|
|
|
983
896
|
</td>
|
|
@@ -987,5 +900,4 @@ The return type of [`createSafeContext`](#createsafecontext)
|
|
|
987
900
|
|
|
988
901
|
### See
|
|
989
902
|
|
|
990
|
-
[`createSafeContext`](#createsafecontext)
|
|
991
|
-
[`RestrictedContext`](#restrictedcontext)
|
|
903
|
+
[`createSafeContext`](#createsafecontext)
|
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,5 +1,17 @@
|
|
|
1
1
|
import { type Context, type Provider } from 'react';
|
|
2
2
|
import type { ArgumentFallback } from '../utils.js';
|
|
3
|
+
/**
|
|
4
|
+
* The return type of {@linkcode createSafeContext}
|
|
5
|
+
*
|
|
6
|
+
* @see
|
|
7
|
+
* {@linkcode createSafeContext},
|
|
8
|
+
* {@linkcode RestrictedContext}
|
|
9
|
+
*/
|
|
10
|
+
export type SafeContext<DisplayName extends string, T> = {
|
|
11
|
+
[K in `${DisplayName}Context`]: RestrictedContext<T>;
|
|
12
|
+
} & {
|
|
13
|
+
[K in `use${DisplayName}`]: () => T;
|
|
14
|
+
};
|
|
3
15
|
/**
|
|
4
16
|
* A React context with a required `displayName` and the obsolete `Consumer`
|
|
5
17
|
* property purposefully omitted so that it is impossible to pass the context
|
|
@@ -16,18 +28,6 @@ export type RestrictedContext<T> = Context<T> extends Provider<T> ? {
|
|
|
16
28
|
Provider: Provider<T>;
|
|
17
29
|
displayName: string;
|
|
18
30
|
};
|
|
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
31
|
/**
|
|
32
32
|
* For a given type `T`, returns a function that produces both a context of that
|
|
33
33
|
* type and a hook that returns the current context value if one was provided,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSafeContext.d.ts","sourceRoot":"","sources":["../../src/misc/createSafeContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,QAAQ,EAA6B,MAAM,OAAO,CAAC;AAC/E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAIpD
|
|
1
|
+
{"version":3,"file":"createSafeContext.d.ts","sourceRoot":"","sources":["../../src/misc/createSafeContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,QAAQ,EAA6B,MAAM,OAAO,CAAC;AAC/E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAIpD;;;;;;GAMG;AACH,MAAM,MAAM,WAAW,CAAC,WAAW,SAAS,MAAM,EAAE,CAAC,IAAI;KACtD,CAAC,IAAI,GAAG,WAAW,SAAS,GAAG,iBAAiB,CAAC,CAAC,CAAC;CACrD,GAAG;KACD,CAAC,IAAI,MAAM,WAAW,EAAE,GAAG,MAAM,CAAC;CACpC,CAAC;AAEF;;;;;;;;GAQG;AAIH,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAC7B,OAAO,CAAC,CAAC,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,GAC1B;IAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,GAC5D;IAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;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,WAAW,CAAC,WAAW,EAAE,CAAC,CAAC,CAsB/B"}
|
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
|
|
@@ -3,6 +3,19 @@ import type { ArgumentFallback } from '../utils.js';
|
|
|
3
3
|
|
|
4
4
|
const moValueSymbol = Symbol('noValue');
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* The return type of {@linkcode createSafeContext}
|
|
8
|
+
*
|
|
9
|
+
* @see
|
|
10
|
+
* {@linkcode createSafeContext},
|
|
11
|
+
* {@linkcode RestrictedContext}
|
|
12
|
+
*/
|
|
13
|
+
export type SafeContext<DisplayName extends string, T> = {
|
|
14
|
+
[K in `${DisplayName}Context`]: RestrictedContext<T>;
|
|
15
|
+
} & {
|
|
16
|
+
[K in `use${DisplayName}`]: () => T;
|
|
17
|
+
};
|
|
18
|
+
|
|
6
19
|
/**
|
|
7
20
|
* A React context with a required `displayName` and the obsolete `Consumer`
|
|
8
21
|
* property purposefully omitted so that it is impossible to pass the context
|
|
@@ -20,19 +33,6 @@ export type RestrictedContext<T> =
|
|
|
20
33
|
? { Provider: Provider<T>; displayName: string } & Provider<T>
|
|
21
34
|
: { Provider: Provider<T>; displayName: string };
|
|
22
35
|
|
|
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
36
|
/**
|
|
37
37
|
* For a given type `T`, returns a function that produces both a context of that
|
|
38
38
|
* type and a hook that returns the current context value if one was provided,
|
|
@@ -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
|
-
}
|