@aweebit/react-essentials 0.8.0 → 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/LICENSE +0 -2
- package/README.md +81 -185
- 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 +12 -11
- package/dist/hooks/useEventListener.d.ts.map +1 -1
- package/dist/hooks/useEventListener.js +3 -7
- package/dist/hooks/useEventListener.js.map +1 -1
- package/dist/hooks/useReducerWithDeps.d.ts +7 -0
- package/dist/hooks/useReducerWithDeps.d.ts.map +1 -1
- package/dist/hooks/useReducerWithDeps.js +10 -3
- package/dist/hooks/useReducerWithDeps.js.map +1 -1
- package/dist/hooks/useStateWithDeps.d.ts +6 -7
- package/dist/hooks/useStateWithDeps.d.ts.map +1 -1
- package/dist/hooks/useStateWithDeps.js +14 -45
- package/dist/hooks/useStateWithDeps.js.map +1 -1
- package/dist/misc/createSafeContext.d.ts +12 -12
- package/dist/misc/createSafeContext.d.ts.map +1 -1
- package/dist/utils.d.ts +0 -3
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +0 -4
- package/dist/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/index.ts +0 -1
- package/src/hooks/useEventListener.ts +21 -17
- package/src/hooks/useReducerWithDeps.ts +10 -3
- package/src/hooks/useStateWithDeps.ts +14 -48
- package/src/misc/createSafeContext.ts +13 -13
- package/src/utils.ts +0 -8
- package/dist/hooks/useForceUpdate.d.ts +0 -75
- package/dist/hooks/useForceUpdate.d.ts.map +0 -1
- package/dist/hooks/useForceUpdate.js +0 -87
- package/dist/hooks/useForceUpdate.js.map +0 -1
- package/src/hooks/useForceUpdate.ts +0 -91
package/LICENSE
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
3
|
Copyright (c) 2025 Wee Bit
|
|
4
|
-
Portions Copyright (c) 2020 Peter Juras
|
|
5
|
-
Portions Copyright (c) 2020 Julien CARON
|
|
6
4
|
|
|
7
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
6
|
of this software and associated documentation files (the "Software"), to deal
|
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:
|
|
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,122 +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:81](https://github.com/aweebit/react-essentials/blob/v0.8.0/src/hooks/useForceUpdate.ts#L81)
|
|
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
|
-
### Example
|
|
90
|
-
|
|
91
|
-
Sometimes, React's immutability constraints mean too much unnecessary copying
|
|
92
|
-
of data when new data arrives at a high frequency. In such cases, it might be
|
|
93
|
-
desirable to ignore the constraints by embracing imperative patterns.
|
|
94
|
-
Here is an example of a scenario where that can make sense:
|
|
95
|
-
|
|
96
|
-
```tsx
|
|
97
|
-
type SensorData = { timestamp: number; value: number };
|
|
98
|
-
const sensorDataRef = useRef<SensorData[]>([]);
|
|
99
|
-
const mostRecentSensorDataTimestampRef = useRef<number>(0);
|
|
100
|
-
|
|
101
|
-
const [forceUpdate, updateCount] = useForceUpdate();
|
|
102
|
-
// Limiting the frequency of forced re-renders with some throttle function:
|
|
103
|
-
const throttledForceUpdateRef = useRef(throttle(forceUpdate));
|
|
104
|
-
|
|
105
|
-
useEffect(() => {
|
|
106
|
-
return sensorDataObservable.subscribe((data: SensorData) => {
|
|
107
|
-
// Imagine new sensor data arrives every 1 millisecond. If we were following
|
|
108
|
-
// React's immutability rules by creating a new array every time, the data
|
|
109
|
-
// that's already there would have to be copied many times before the new
|
|
110
|
-
// data would even get a chance to be reflected in the UI for the first time
|
|
111
|
-
// because it typically takes much longer than 1 millisecond for a new frame
|
|
112
|
-
// to be displayed. To prevent the waste of computational resources, we just
|
|
113
|
-
// mutate the existing array every time instead:
|
|
114
|
-
sensorDataRef.current.push(data);
|
|
115
|
-
if (data.timestamp > mostRecentSensorDataTimestampRef.current) {
|
|
116
|
-
mostRecentSensorDataTimestampRef.current = data.timestamp;
|
|
117
|
-
}
|
|
118
|
-
throttledForceUpdateRef.current();
|
|
119
|
-
});
|
|
120
|
-
}, []);
|
|
121
|
-
|
|
122
|
-
const [timeWindow, setTimeWindow] = useState(1000);
|
|
123
|
-
const selectedSensorData = useMemo(
|
|
124
|
-
() => {
|
|
125
|
-
// Keep this line if you don't want to disable the
|
|
126
|
-
// react-hooks/exhaustive-deps ESLint rule:
|
|
127
|
-
updateCount;
|
|
128
|
-
const threshold = mostRecentSensorDataTimestampRef.current - timeWindow;
|
|
129
|
-
return sensorDataRef.current.filter(
|
|
130
|
-
({ timestamp }) => timestamp >= threshold,
|
|
131
|
-
);
|
|
132
|
-
},
|
|
133
|
-
// sensorDataRef.current always references the same array, so listing it as a
|
|
134
|
-
// dependency is pointless. Instead, updateCount should be used:
|
|
135
|
-
[updateCount, timeWindow],
|
|
136
|
-
);
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
### Parameters
|
|
140
|
-
|
|
141
|
-
<table>
|
|
142
|
-
<thead>
|
|
143
|
-
<tr>
|
|
144
|
-
<th>Parameter</th>
|
|
145
|
-
<th>Type</th>
|
|
146
|
-
<th>Description</th>
|
|
147
|
-
</tr>
|
|
148
|
-
</thead>
|
|
149
|
-
<tbody>
|
|
150
|
-
<tr>
|
|
151
|
-
<td>
|
|
152
|
-
|
|
153
|
-
`callback?`
|
|
154
|
-
|
|
155
|
-
</td>
|
|
156
|
-
<td>
|
|
157
|
-
|
|
158
|
-
() => `void`
|
|
159
|
-
|
|
160
|
-
</td>
|
|
161
|
-
<td>
|
|
162
|
-
|
|
163
|
-
An optional callback function to call during renders that
|
|
164
|
-
were triggered with `forceUpdate()`
|
|
165
|
-
|
|
166
|
-
Can be used for conditionally calling state setters when state needs to be
|
|
167
|
-
reset. That is legal and better than using effects (see
|
|
168
|
-
[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)),
|
|
169
|
-
but can often be avoided by using [`useStateWithDeps`](#usestatewithdeps) or
|
|
170
|
-
[`useReducerWithDeps`](#usereducerwithdeps).
|
|
171
|
-
|
|
172
|
-
Important: the callback function is called once per render, not once per
|
|
173
|
-
`forceUpdate` call! If React batches `forceUpdate` calls, then it will only
|
|
174
|
-
be called once.
|
|
175
|
-
|
|
176
|
-
</td>
|
|
177
|
-
</tr>
|
|
178
|
-
</tbody>
|
|
179
|
-
</table>
|
|
180
|
-
|
|
181
|
-
### Returns
|
|
182
|
-
|
|
183
|
-
\[() => `void`, `bigint`\]
|
|
184
|
-
|
|
185
|
-
An array with the following two elements:
|
|
186
|
-
|
|
187
|
-
1. A `forceUpdate` function that triggers a re-render
|
|
188
|
-
2. The number of times `forceUpdate` has been called so far
|
|
189
|
-
|
|
190
|
-
---
|
|
191
|
-
|
|
192
69
|
## useReducerWithDeps()
|
|
193
70
|
|
|
194
71
|
```ts
|
|
@@ -199,11 +76,18 @@ function useReducerWithDeps<S, A>(
|
|
|
199
76
|
): [S, ActionDispatch<A>];
|
|
200
77
|
```
|
|
201
78
|
|
|
202
|
-
Defined in: [hooks/useReducerWithDeps.ts:
|
|
79
|
+
Defined in: [hooks/useReducerWithDeps.ts:59](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useReducerWithDeps.ts#L59)
|
|
203
80
|
|
|
204
81
|
`useReducer` hook with an additional dependency array `deps` that resets the
|
|
205
82
|
state to `initialState` when dependencies change
|
|
206
83
|
|
|
84
|
+
This hook is the reducer pattern counterpart of [`useStateWithDeps`](#usestatewithdeps).
|
|
85
|
+
|
|
86
|
+
Due to React's limitations, a change in dependencies always causes two
|
|
87
|
+
renders when using this hook. The result of the first render is thrown away
|
|
88
|
+
as described in
|
|
89
|
+
[useState > Storing information from previous renders](https://react.dev/reference/react/useState#storing-information-from-previous-renders).
|
|
90
|
+
|
|
207
91
|
For motivation and examples, see
|
|
208
92
|
https://github.com/facebook/react/issues/33041.
|
|
209
93
|
|
|
@@ -335,11 +219,16 @@ function useStateWithDeps<S>(
|
|
|
335
219
|
): [S, Dispatch<SetStateAction<S>>];
|
|
336
220
|
```
|
|
337
221
|
|
|
338
|
-
Defined in: [hooks/useStateWithDeps.ts:
|
|
222
|
+
Defined in: [hooks/useStateWithDeps.ts:62](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useStateWithDeps.ts#L62)
|
|
339
223
|
|
|
340
224
|
`useState` hook with an additional dependency array `deps` that resets the
|
|
341
225
|
state to `initialState` when dependencies change
|
|
342
226
|
|
|
227
|
+
Due to React's limitations, a change in dependencies always causes two
|
|
228
|
+
renders when using this hook. The result of the first render is thrown away
|
|
229
|
+
as described in
|
|
230
|
+
[useState > Storing information from previous renders](https://react.dev/reference/react/useState#storing-information-from-previous-renders).
|
|
231
|
+
|
|
343
232
|
For motivation and more examples, see
|
|
344
233
|
https://github.com/facebook/react/issues/33041.
|
|
345
234
|
|
|
@@ -359,7 +248,7 @@ const activityOptionsByTimeOfDay: {
|
|
|
359
248
|
evening: ['board games', 'dinner'],
|
|
360
249
|
};
|
|
361
250
|
|
|
362
|
-
|
|
251
|
+
function Example() {
|
|
363
252
|
const [timeOfDay, setTimeOfDay] = useState<TimeOfDay>('morning');
|
|
364
253
|
|
|
365
254
|
const activityOptions = activityOptionsByTimeOfDay[timeOfDay];
|
|
@@ -462,7 +351,7 @@ function createSafeContext<T>(): <DisplayName>(
|
|
|
462
351
|
) => SafeContext<DisplayName, T>;
|
|
463
352
|
```
|
|
464
353
|
|
|
465
|
-
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)
|
|
466
355
|
|
|
467
356
|
For a given type `T`, returns a function that produces both a context of that
|
|
468
357
|
type and a hook that returns the current context value if one was provided,
|
|
@@ -609,15 +498,11 @@ A function that accepts a single string argument `displayName` (e.g.
|
|
|
609
498
|
|
|
610
499
|
```ts
|
|
611
500
|
type UseEventListener = UseEventListenerWithImplicitWindowTarget &
|
|
612
|
-
|
|
613
|
-
UseEventListenerWithExplicitTarget<Document, DocumentEventMap> &
|
|
614
|
-
UseEventListenerWithExplicitTarget<HTMLElement, HTMLElementEventMap> &
|
|
615
|
-
UseEventListenerWithExplicitTarget<SVGElement, SVGElementEventMap> &
|
|
616
|
-
UseEventListenerWithExplicitTarget<MathMLElement, MathMLElementEventMap> &
|
|
501
|
+
UseEventListenerWithExplicitGlobalTarget &
|
|
617
502
|
UseEventListenerWithAnyExplicitTarget;
|
|
618
503
|
```
|
|
619
504
|
|
|
620
|
-
Defined in: [hooks/useEventListener.ts:
|
|
505
|
+
Defined in: [hooks/useEventListener.ts:12](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L12)
|
|
621
506
|
|
|
622
507
|
The type of [`useEventListener`](#useeventlistener-1)
|
|
623
508
|
|
|
@@ -625,7 +510,7 @@ The type of [`useEventListener`](#useeventlistener-1)
|
|
|
625
510
|
|
|
626
511
|
[`useEventListener`](#useeventlistener-1),
|
|
627
512
|
[`UseEventListenerWithImplicitWindowTarget`](#useeventlistenerwithimplicitwindowtarget),
|
|
628
|
-
[`
|
|
513
|
+
[`UseEventListenerWithExplicitGlobalTarget`](#useeventlistenerwithexplicitglobaltarget),
|
|
629
514
|
[`UseEventListenerWithAnyExplicitTarget`](#useeventlistenerwithanyexplicittarget)
|
|
630
515
|
|
|
631
516
|
---
|
|
@@ -636,7 +521,7 @@ The type of [`useEventListener`](#useeventlistener-1)
|
|
|
636
521
|
type UseEventListenerWithImplicitWindowTarget = <K>(...args) => void;
|
|
637
522
|
```
|
|
638
523
|
|
|
639
|
-
Defined in: [hooks/useEventListener.ts:
|
|
524
|
+
Defined in: [hooks/useEventListener.ts:21](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L21)
|
|
640
525
|
|
|
641
526
|
### Type Parameters
|
|
642
527
|
|
|
@@ -693,6 +578,26 @@ Defined in: [hooks/useEventListener.ts:31](https://github.com/aweebit/react-esse
|
|
|
693
578
|
|
|
694
579
|
---
|
|
695
580
|
|
|
581
|
+
## UseEventListenerWithExplicitGlobalTarget
|
|
582
|
+
|
|
583
|
+
```ts
|
|
584
|
+
type UseEventListenerWithExplicitGlobalTarget =
|
|
585
|
+
UseEventListenerWithExplicitTarget<Window, WindowEventMap> &
|
|
586
|
+
UseEventListenerWithExplicitTarget<Document, DocumentEventMap> &
|
|
587
|
+
UseEventListenerWithExplicitTarget<HTMLElement, HTMLElementEventMap> &
|
|
588
|
+
UseEventListenerWithExplicitTarget<SVGElement, SVGElementEventMap> &
|
|
589
|
+
UseEventListenerWithExplicitTarget<MathMLElement, MathMLElementEventMap>;
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
Defined in: [hooks/useEventListener.ts:32](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L32)
|
|
593
|
+
|
|
594
|
+
### See
|
|
595
|
+
|
|
596
|
+
[`useEventListener`](#useeventlistener-1),
|
|
597
|
+
[`UseEventListenerWithExplicitTarget`](#useeventlistenerwithexplicittarget)
|
|
598
|
+
|
|
599
|
+
---
|
|
600
|
+
|
|
696
601
|
## UseEventListenerWithExplicitTarget()
|
|
697
602
|
|
|
698
603
|
```ts
|
|
@@ -701,7 +606,7 @@ type UseEventListenerWithExplicitTarget<Target, EventMap> = <T, K>(
|
|
|
701
606
|
) => void;
|
|
702
607
|
```
|
|
703
608
|
|
|
704
|
-
Defined in: [hooks/useEventListener.ts:
|
|
609
|
+
Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L44)
|
|
705
610
|
|
|
706
611
|
### Type Parameters
|
|
707
612
|
|
|
@@ -709,7 +614,6 @@ Defined in: [hooks/useEventListener.ts:42](https://github.com/aweebit/react-esse
|
|
|
709
614
|
<thead>
|
|
710
615
|
<tr>
|
|
711
616
|
<th>Type Parameter</th>
|
|
712
|
-
<th>Default type</th>
|
|
713
617
|
</tr>
|
|
714
618
|
</thead>
|
|
715
619
|
<tbody>
|
|
@@ -718,11 +622,6 @@ Defined in: [hooks/useEventListener.ts:42](https://github.com/aweebit/react-esse
|
|
|
718
622
|
|
|
719
623
|
`Target` _extends_ `EventTarget`
|
|
720
624
|
|
|
721
|
-
</td>
|
|
722
|
-
<td>
|
|
723
|
-
|
|
724
|
-
‐
|
|
725
|
-
|
|
726
625
|
</td>
|
|
727
626
|
</tr>
|
|
728
627
|
<tr>
|
|
@@ -730,11 +629,6 @@ Defined in: [hooks/useEventListener.ts:42](https://github.com/aweebit/react-esse
|
|
|
730
629
|
|
|
731
630
|
`EventMap`
|
|
732
631
|
|
|
733
|
-
</td>
|
|
734
|
-
<td>
|
|
735
|
-
|
|
736
|
-
`Record`\<`string`, `Event`\>
|
|
737
|
-
|
|
738
632
|
</td>
|
|
739
633
|
</tr>
|
|
740
634
|
</tbody>
|
|
@@ -805,11 +699,13 @@ Defined in: [hooks/useEventListener.ts:42](https://github.com/aweebit/react-esse
|
|
|
805
699
|
## UseEventListenerWithAnyExplicitTarget
|
|
806
700
|
|
|
807
701
|
```ts
|
|
808
|
-
type UseEventListenerWithAnyExplicitTarget =
|
|
809
|
-
|
|
702
|
+
type UseEventListenerWithAnyExplicitTarget = UseEventListenerWithExplicitTarget<
|
|
703
|
+
EventTarget,
|
|
704
|
+
Record<string, Event>
|
|
705
|
+
>;
|
|
810
706
|
```
|
|
811
707
|
|
|
812
|
-
Defined in: [hooks/useEventListener.ts:
|
|
708
|
+
Defined in: [hooks/useEventListener.ts:56](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L56)
|
|
813
709
|
|
|
814
710
|
### See
|
|
815
711
|
|
|
@@ -830,7 +726,7 @@ type UseEventListenerWithImplicitWindowTargetArgs<K> =
|
|
|
830
726
|
: never;
|
|
831
727
|
```
|
|
832
728
|
|
|
833
|
-
Defined in: [hooks/useEventListener.ts:
|
|
729
|
+
Defined in: [hooks/useEventListener.ts:64](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L64)
|
|
834
730
|
|
|
835
731
|
### Type Parameters
|
|
836
732
|
|
|
@@ -875,7 +771,7 @@ type UseEventListenerWithExplicitTargetArgs<EventMap, T, K> = [
|
|
|
875
771
|
];
|
|
876
772
|
```
|
|
877
773
|
|
|
878
|
-
Defined in: [hooks/useEventListener.ts:
|
|
774
|
+
Defined in: [hooks/useEventListener.ts:78](https://github.com/aweebit/react-essentials/blob/v0.9.0/src/hooks/useEventListener.ts#L78)
|
|
879
775
|
|
|
880
776
|
### Type Parameters
|
|
881
777
|
|
|
@@ -916,27 +812,17 @@ Defined in: [hooks/useEventListener.ts:76](https://github.com/aweebit/react-esse
|
|
|
916
812
|
|
|
917
813
|
---
|
|
918
814
|
|
|
919
|
-
##
|
|
815
|
+
## SafeContext
|
|
920
816
|
|
|
921
817
|
```ts
|
|
922
|
-
type
|
|
923
|
-
Context
|
|
924
|
-
|
|
925
|
-
Provider: Provider<T>;
|
|
926
|
-
displayName: string;
|
|
927
|
-
} & Provider<T>
|
|
928
|
-
: {
|
|
929
|
-
Provider: Provider<T>;
|
|
930
|
-
displayName: string;
|
|
931
|
-
};
|
|
818
|
+
type SafeContext<DisplayName, T> = {
|
|
819
|
+
[K in `${DisplayName}Context`]: RestrictedContext<T>;
|
|
820
|
+
} & { [K in `use${DisplayName}`]: () => T };
|
|
932
821
|
```
|
|
933
822
|
|
|
934
|
-
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)
|
|
935
824
|
|
|
936
|
-
|
|
937
|
-
property purposefully omitted so that it is impossible to pass the context
|
|
938
|
-
as an argument to `useContext` or `use` (the hook produced with
|
|
939
|
-
[`createSafeContext`](#createsafecontext) should be used instead)
|
|
825
|
+
The return type of [`createSafeContext`](#createsafecontext)
|
|
940
826
|
|
|
941
827
|
### Type Parameters
|
|
942
828
|
|
|
@@ -950,6 +836,13 @@ as an argument to `useContext` or `use` (the hook produced with
|
|
|
950
836
|
<tr>
|
|
951
837
|
<td>
|
|
952
838
|
|
|
839
|
+
`DisplayName` _extends_ `string`
|
|
840
|
+
|
|
841
|
+
</td>
|
|
842
|
+
</tr>
|
|
843
|
+
<tr>
|
|
844
|
+
<td>
|
|
845
|
+
|
|
953
846
|
`T`
|
|
954
847
|
|
|
955
848
|
</td>
|
|
@@ -959,21 +852,32 @@ as an argument to `useContext` or `use` (the hook produced with
|
|
|
959
852
|
|
|
960
853
|
### See
|
|
961
854
|
|
|
962
|
-
[`createSafeContext`](#createsafecontext)
|
|
855
|
+
[`createSafeContext`](#createsafecontext),
|
|
856
|
+
[`RestrictedContext`](#restrictedcontext)
|
|
963
857
|
|
|
964
858
|
---
|
|
965
859
|
|
|
966
|
-
##
|
|
860
|
+
## RestrictedContext
|
|
967
861
|
|
|
968
862
|
```ts
|
|
969
|
-
type
|
|
970
|
-
|
|
971
|
-
|
|
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
|
+
};
|
|
972
873
|
```
|
|
973
874
|
|
|
974
|
-
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)
|
|
975
876
|
|
|
976
|
-
|
|
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)
|
|
977
881
|
|
|
978
882
|
### Type Parameters
|
|
979
883
|
|
|
@@ -987,13 +891,6 @@ The return type of [`createSafeContext`](#createsafecontext)
|
|
|
987
891
|
<tr>
|
|
988
892
|
<td>
|
|
989
893
|
|
|
990
|
-
`DisplayName` _extends_ `string`
|
|
991
|
-
|
|
992
|
-
</td>
|
|
993
|
-
</tr>
|
|
994
|
-
<tr>
|
|
995
|
-
<td>
|
|
996
|
-
|
|
997
894
|
`T`
|
|
998
895
|
|
|
999
896
|
</td>
|
|
@@ -1003,5 +900,4 @@ The return type of [`createSafeContext`](#createsafecontext)
|
|
|
1003
900
|
|
|
1004
901
|
### See
|
|
1005
902
|
|
|
1006
|
-
[`createSafeContext`](#createsafecontext)
|
|
1007
|
-
[`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"}
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Based on {@link https://github.com/juliencrn/usehooks-ts}
|
|
3
|
-
*
|
|
4
|
-
* @license MIT
|
|
5
|
-
* @copyright 2020 Julien CARON
|
|
6
|
-
*/
|
|
7
1
|
import { type RefObject } from 'react';
|
|
8
2
|
/**
|
|
9
3
|
* The type of {@linkcode useEventListener}
|
|
@@ -11,27 +5,34 @@ import { type RefObject } from 'react';
|
|
|
11
5
|
* @see
|
|
12
6
|
* {@linkcode useEventListener},
|
|
13
7
|
* {@linkcode UseEventListenerWithImplicitWindowTarget},
|
|
14
|
-
* {@linkcode
|
|
8
|
+
* {@linkcode UseEventListenerWithExplicitGlobalTarget},
|
|
15
9
|
* {@linkcode UseEventListenerWithAnyExplicitTarget}
|
|
16
10
|
*/
|
|
17
|
-
export type UseEventListener = UseEventListenerWithImplicitWindowTarget &
|
|
11
|
+
export type UseEventListener = UseEventListenerWithImplicitWindowTarget & UseEventListenerWithExplicitGlobalTarget & UseEventListenerWithAnyExplicitTarget;
|
|
18
12
|
/**
|
|
19
13
|
* @see
|
|
20
14
|
* {@linkcode useEventListener},
|
|
21
|
-
* {@linkcode UseEventListenerWithImplicitWindowTargetArgs}
|
|
15
|
+
* {@linkcode UseEventListenerWithImplicitWindowTargetArgs}
|
|
16
|
+
*/
|
|
22
17
|
export type UseEventListenerWithImplicitWindowTarget = <K extends keyof WindowEventMap>(...args: UseEventListenerWithImplicitWindowTargetArgs<K>) => void;
|
|
18
|
+
/**
|
|
19
|
+
* @see
|
|
20
|
+
* {@linkcode useEventListener},
|
|
21
|
+
* {@linkcode UseEventListenerWithExplicitTarget}
|
|
22
|
+
*/
|
|
23
|
+
export type UseEventListenerWithExplicitGlobalTarget = UseEventListenerWithExplicitTarget<Window, WindowEventMap> & UseEventListenerWithExplicitTarget<Document, DocumentEventMap> & UseEventListenerWithExplicitTarget<HTMLElement, HTMLElementEventMap> & UseEventListenerWithExplicitTarget<SVGElement, SVGElementEventMap> & UseEventListenerWithExplicitTarget<MathMLElement, MathMLElementEventMap>;
|
|
23
24
|
/**
|
|
24
25
|
* @see
|
|
25
26
|
* {@linkcode useEventListener},
|
|
26
27
|
* {@linkcode UseEventListenerWithExplicitTargetArgs}
|
|
27
28
|
*/
|
|
28
|
-
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;
|
|
29
30
|
/**
|
|
30
31
|
* @see
|
|
31
32
|
* {@linkcode useEventListener},
|
|
32
33
|
* {@linkcode UseEventListenerWithExplicitTarget}
|
|
33
34
|
*/
|
|
34
|
-
export type UseEventListenerWithAnyExplicitTarget = UseEventListenerWithExplicitTarget<EventTarget
|
|
35
|
+
export type UseEventListenerWithAnyExplicitTarget = UseEventListenerWithExplicitTarget<EventTarget, Record<string, Event>>;
|
|
35
36
|
/**
|
|
36
37
|
* @see
|
|
37
38
|
* {@linkcode useEventListener},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useEventListener.d.ts","sourceRoot":"","sources":["../../src/hooks/useEventListener.ts"],"names":[],"mappings":"AAAA
|
|
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,9 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Based on {@link https://github.com/juliencrn/usehooks-ts}
|
|
3
|
-
*
|
|
4
|
-
* @license MIT
|
|
5
|
-
* @copyright 2020 Julien CARON
|
|
6
|
-
*/
|
|
7
1
|
import { useEffect, useMemo, useRef } from 'react';
|
|
8
2
|
/**
|
|
9
3
|
* Adds `handler` as a listener for the event `eventName` of `target` with the
|
|
@@ -47,7 +41,9 @@ export const useEventListener = function useEventListener(...args) {
|
|
|
47
41
|
: args;
|
|
48
42
|
const unwrappedTarget = target && !('addEventListener' in target) ? target.current : target;
|
|
49
43
|
const handlerRef = useRef(handler);
|
|
50
|
-
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
handlerRef.current = handler;
|
|
46
|
+
}, [handler]);
|
|
51
47
|
const { capture = false, once = false, passive, signal, } = typeof options === 'boolean' ? { capture: options } : (options ?? {});
|
|
52
48
|
const memoizedOptions = useMemo(() => options,
|
|
53
49
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useEventListener.js","sourceRoot":"","sources":["../../src/hooks/useEventListener.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"useEventListener.js","sourceRoot":"","sources":["../../src/hooks/useEventListener.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAkB,MAAM,OAAO,CAAC;AAkGnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAqB,SAAS,gBAAgB,CACzE,GAAG,IAE0C;IAE7C,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,GAMzC,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ;QACzB,CAAC,CAAC,CAAC,MAAM,EAAE,GAAI,IAAwD,CAAC;QACxE,CAAC,CAAE,IAAkD,CAAC;IAE1D,MAAM,eAAe,GACnB,MAAM,IAAI,CAAC,CAAC,kBAAkB,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC;IAEtE,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IACnC,SAAS,CAAC,GAAG,EAAE;QACb,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC;IAC/B,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAEd,MAAM,EACJ,OAAO,GAAG,KAAK,EACf,IAAI,GAAG,KAAK,EACZ,OAAO,EACP,MAAM,GACP,GAAG,OAAO,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;IAE1E,MAAM,eAAe,GAAG,OAAO,CAC7B,GAAG,EAAE,CAAC,OAAO;IACb,uDAAuD;IACvD,CAAC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CACjC,CAAC;IAEF,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,eAAe,KAAK,IAAI,EAAE,CAAC;YAC7B,8CAA8C;YAC9C,OAAO;QACT,CAAC;QAED,MAAM,QAAQ,GAAmB,UAAU,KAAK;YAC9C,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QACvC,CAAC,CAAC;QAEF,eAAe,CAAC,gBAAgB,CAAC,SAAS,EAAE,QAAQ,EAAE,eAAe,CAAC,CAAC;QAEvE,OAAO,GAAG,EAAE;YACV,eAAe,CAAC,mBAAmB,CAAC,SAAS,EAAE,QAAQ,EAAE,eAAe,CAAC,CAAC;QAC5E,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,eAAe,EAAE,SAAS,EAAE,eAAe,CAAC,CAAC,CAAC;AACpD,CAAqB,CAAC"}
|
|
@@ -7,6 +7,13 @@ export type ActionDispatch<ActionArg extends AnyActionArg> = (...args: ActionArg
|
|
|
7
7
|
* `useReducer` hook with an additional dependency array `deps` that resets the
|
|
8
8
|
* state to `initialState` when dependencies change
|
|
9
9
|
*
|
|
10
|
+
* This hook is the reducer pattern counterpart of {@linkcode useStateWithDeps}.
|
|
11
|
+
*
|
|
12
|
+
* Due to React's limitations, a change in dependencies always causes two
|
|
13
|
+
* renders when using this hook. The result of the first render is thrown away
|
|
14
|
+
* as described in
|
|
15
|
+
* [useState > Storing information from previous renders](https://react.dev/reference/react/useState#storing-information-from-previous-renders).
|
|
16
|
+
*
|
|
10
17
|
* For motivation and examples, see
|
|
11
18
|
* https://github.com/facebook/react/issues/33041.
|
|
12
19
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useReducerWithDeps.d.ts","sourceRoot":"","sources":["../../src/hooks/useReducerWithDeps.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAU,MAAM,OAAO,CAAC;AAOpD,cAAc;AAEd,MAAM,MAAM,YAAY,GAAG,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;AAEtC,cAAc;AACd,MAAM,MAAM,cAAc,CAAC,SAAS,SAAS,YAAY,IAAI,CAC3D,GAAG,IAAI,EAAE,SAAS,KACf,IAAI,CAAC;AAEV
|
|
1
|
+
{"version":3,"file":"useReducerWithDeps.d.ts","sourceRoot":"","sources":["../../src/hooks/useReducerWithDeps.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAU,MAAM,OAAO,CAAC;AAOpD,cAAc;AAEd,MAAM,MAAM,YAAY,GAAG,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;AAEtC,cAAc;AACd,MAAM,MAAM,cAAc,CAAC,SAAS,SAAS,YAAY,IAAI,CAC3D,GAAG,IAAI,EAAE,SAAS,KACf,IAAI,CAAC;AAEV;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,CAAC,SAAS,YAAY,EAC1D,OAAO,EAAE,CAAC,SAAS,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,EACxC,YAAY,EAAE,CAAC,GAAG,CAAC,CAAC,aAAa,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,EAC5C,IAAI,EAAE,cAAc,GACnB,CAAC,CAAC,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC,CAYxB"}
|
|
@@ -4,6 +4,13 @@ import { useStateWithDeps } from './useStateWithDeps.js';
|
|
|
4
4
|
* `useReducer` hook with an additional dependency array `deps` that resets the
|
|
5
5
|
* state to `initialState` when dependencies change
|
|
6
6
|
*
|
|
7
|
+
* This hook is the reducer pattern counterpart of {@linkcode useStateWithDeps}.
|
|
8
|
+
*
|
|
9
|
+
* Due to React's limitations, a change in dependencies always causes two
|
|
10
|
+
* renders when using this hook. The result of the first render is thrown away
|
|
11
|
+
* as described in
|
|
12
|
+
* [useState > Storing information from previous renders](https://react.dev/reference/react/useState#storing-information-from-previous-renders).
|
|
13
|
+
*
|
|
7
14
|
* For motivation and examples, see
|
|
8
15
|
* https://github.com/facebook/react/issues/33041.
|
|
9
16
|
*
|
|
@@ -40,9 +47,9 @@ export function useReducerWithDeps(reducer, initialState, deps) {
|
|
|
40
47
|
const [state, setState] = useStateWithDeps(initialState, deps);
|
|
41
48
|
// Only the initially provided reducer is used
|
|
42
49
|
const reducerRef = useRef(reducer);
|
|
43
|
-
|
|
50
|
+
function dispatch(...args) {
|
|
44
51
|
setState((previousState) => reducerRef.current(previousState, ...args));
|
|
45
|
-
}
|
|
46
|
-
return [state, dispatch];
|
|
52
|
+
}
|
|
53
|
+
return [state, useRef(dispatch).current];
|
|
47
54
|
}
|
|
48
55
|
//# sourceMappingURL=useReducerWithDeps.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useReducerWithDeps.js","sourceRoot":"","sources":["../../src/hooks/useReducerWithDeps.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,MAAM,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAezD
|
|
1
|
+
{"version":3,"file":"useReducerWithDeps.js","sourceRoot":"","sources":["../../src/hooks/useReducerWithDeps.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,MAAM,EAAE,MAAM,OAAO,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAezD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAM,UAAU,kBAAkB,CAChC,OAAwC,EACxC,YAA4C,EAC5C,IAAoB;IAEpB,uDAAuD;IACvD,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,gBAAgB,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;IAE/D,8CAA8C;IAC9C,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IAEnC,SAAS,QAAQ,CAAC,GAAG,IAAO;QAC1B,QAAQ,CAAC,CAAC,aAAa,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,aAAa,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;IAC1E,CAAC;IAED,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC;AAC3C,CAAC"}
|
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Based on {@link https://github.com/peterjuras/use-state-with-deps}
|
|
3
|
-
*
|
|
4
|
-
* @license MIT
|
|
5
|
-
* @copyright 2020 Peter Juras
|
|
6
|
-
*/
|
|
7
1
|
import { type DependencyList, type Dispatch, type SetStateAction } from 'react';
|
|
8
2
|
/**
|
|
9
3
|
* `useState` hook with an additional dependency array `deps` that resets the
|
|
10
4
|
* state to `initialState` when dependencies change
|
|
11
5
|
*
|
|
6
|
+
* Due to React's limitations, a change in dependencies always causes two
|
|
7
|
+
* renders when using this hook. The result of the first render is thrown away
|
|
8
|
+
* as described in
|
|
9
|
+
* [useState > Storing information from previous renders](https://react.dev/reference/react/useState#storing-information-from-previous-renders).
|
|
10
|
+
*
|
|
12
11
|
* For motivation and more examples, see
|
|
13
12
|
* https://github.com/facebook/react/issues/33041.
|
|
14
13
|
*
|
|
@@ -27,7 +26,7 @@ import { type DependencyList, type Dispatch, type SetStateAction } from 'react';
|
|
|
27
26
|
* evening: ['board games', 'dinner'],
|
|
28
27
|
* };
|
|
29
28
|
*
|
|
30
|
-
*
|
|
29
|
+
* function Example() {
|
|
31
30
|
* const [timeOfDay, setTimeOfDay] = useState<TimeOfDay>('morning');
|
|
32
31
|
*
|
|
33
32
|
* const activityOptions = activityOptionsByTimeOfDay[timeOfDay];
|