@aweebit/react-essentials 0.7.0 → 0.8.1
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 +392 -237
- package/dist/hooks/useEventListener.d.ts +63 -43
- package/dist/hooks/useEventListener.d.ts.map +1 -1
- package/dist/hooks/useEventListener.js +26 -26
- package/dist/hooks/useEventListener.js.map +1 -1
- package/dist/hooks/useForceUpdate.d.ts +4 -48
- package/dist/hooks/useForceUpdate.d.ts.map +1 -1
- package/dist/hooks/useForceUpdate.js +4 -48
- package/dist/hooks/useForceUpdate.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 +11 -4
- 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 +8 -2
- package/dist/misc/createSafeContext.d.ts.map +1 -1
- package/dist/misc/createSafeContext.js +3 -0
- package/dist/misc/createSafeContext.js.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/useEventListener.ts +110 -124
- package/src/hooks/useForceUpdate.ts +4 -48
- package/src/hooks/useReducerWithDeps.ts +11 -4
- package/src/hooks/useStateWithDeps.ts +14 -49
- package/src/misc/createSafeContext.ts +8 -2
- package/src/utils.ts +0 -8
package/README.md
CHANGED
|
@@ -2,19 +2,50 @@
|
|
|
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
|
+
|
|
5
12
|
- [useEventListener()](#useeventlistener)
|
|
6
13
|
- [useForceUpdate()](#useforceupdate)
|
|
7
14
|
- [useReducerWithDeps()](#usereducerwithdeps)
|
|
8
15
|
- [useStateWithDeps()](#usestatewithdeps)
|
|
9
16
|
- [createSafeContext()](#createsafecontext)
|
|
17
|
+
|
|
18
|
+
### Types
|
|
19
|
+
|
|
20
|
+
- [UseEventListener](#useeventlistener-1)
|
|
21
|
+
- [UseEventListenerWithImplicitWindowTarget](#useeventlistenerwithimplicitwindowtarget)
|
|
22
|
+
- [UseEventListenerWithExplicitTarget](#useeventlistenerwithexplicittarget)
|
|
23
|
+
- [UseEventListenerWithAnyExplicitTarget](#useeventlistenerwithanyexplicittarget)
|
|
24
|
+
- [UseEventListenerWithImplicitWindowTargetArgs](#useeventlistenerwithimplicitwindowtargetargs)
|
|
25
|
+
- [UseEventListenerWithExplicitTargetArgs](#useeventlistenerwithexplicittargetargs)
|
|
10
26
|
- [RestrictedContext](#restrictedcontext)
|
|
11
27
|
- [SafeContext](#safecontext)
|
|
12
28
|
|
|
13
|
-
## useEventListener
|
|
29
|
+
## useEventListener
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
const useEventListener: UseEventListener;
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Defined in: [hooks/useEventListener.ts:135](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/hooks/useEventListener.ts#L135)
|
|
14
36
|
|
|
15
37
|
Adds `handler` as a listener for the event `eventName` of `target` with the
|
|
16
38
|
provided `options` applied
|
|
17
39
|
|
|
40
|
+
The following call signatures are available:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
function useEventListener(eventName, handler, options?): void;
|
|
44
|
+
function useEventListener(target, eventName, handler, options?): void;
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
For the full definition of the hook's type, see [`UseEventListener`](#useeventlistener).
|
|
48
|
+
|
|
18
49
|
If `target` is not provided, `window` is used instead.
|
|
19
50
|
|
|
20
51
|
If `target` is `null`, no event listener is added. This is useful when
|
|
@@ -33,253 +64,33 @@ useEventListener(document, 'visibilitychange', () => {
|
|
|
33
64
|
});
|
|
34
65
|
|
|
35
66
|
const buttonRef = useRef<HTMLButtonElement>(null);
|
|
36
|
-
useEventListener(buttonRef
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
### Call Signature
|
|
40
|
-
|
|
41
|
-
```ts
|
|
42
|
-
function useEventListener<K>(eventName, handler, options?): void;
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
Defined in: [hooks/useEventListener.ts:99](https://github.com/aweebit/react-essentials/blob/v0.7.0/src/hooks/useEventListener.ts#L99)
|
|
46
|
-
|
|
47
|
-
#### Type Parameters
|
|
48
|
-
|
|
49
|
-
<table>
|
|
50
|
-
<thead>
|
|
51
|
-
<tr>
|
|
52
|
-
<th>Type Parameter</th>
|
|
53
|
-
</tr>
|
|
54
|
-
</thead>
|
|
55
|
-
<tbody>
|
|
56
|
-
<tr>
|
|
57
|
-
<td>
|
|
58
|
-
|
|
59
|
-
`K` _extends_ keyof `WindowEventMap`
|
|
60
|
-
|
|
61
|
-
</td>
|
|
62
|
-
</tr>
|
|
63
|
-
</tbody>
|
|
64
|
-
</table>
|
|
65
|
-
|
|
66
|
-
#### Parameters
|
|
67
|
-
|
|
68
|
-
<table>
|
|
69
|
-
<thead>
|
|
70
|
-
<tr>
|
|
71
|
-
<th>Parameter</th>
|
|
72
|
-
<th>Type</th>
|
|
73
|
-
</tr>
|
|
74
|
-
</thead>
|
|
75
|
-
<tbody>
|
|
76
|
-
<tr>
|
|
77
|
-
<td>
|
|
78
|
-
|
|
79
|
-
`eventName`
|
|
80
|
-
|
|
81
|
-
</td>
|
|
82
|
-
<td>
|
|
83
|
-
|
|
84
|
-
`K`
|
|
85
|
-
|
|
86
|
-
</td>
|
|
87
|
-
</tr>
|
|
88
|
-
<tr>
|
|
89
|
-
<td>
|
|
90
|
-
|
|
91
|
-
`handler`
|
|
92
|
-
|
|
93
|
-
</td>
|
|
94
|
-
<td>
|
|
95
|
-
|
|
96
|
-
(`this`, `event`) => `void`
|
|
97
|
-
|
|
98
|
-
</td>
|
|
99
|
-
</tr>
|
|
100
|
-
<tr>
|
|
101
|
-
<td>
|
|
102
|
-
|
|
103
|
-
`options?`
|
|
104
|
-
|
|
105
|
-
</td>
|
|
106
|
-
<td>
|
|
107
|
-
|
|
108
|
-
`boolean` \| `AddEventListenerOptions`
|
|
109
|
-
|
|
110
|
-
</td>
|
|
111
|
-
</tr>
|
|
112
|
-
</tbody>
|
|
113
|
-
</table>
|
|
114
|
-
|
|
115
|
-
#### Returns
|
|
116
|
-
|
|
117
|
-
`void`
|
|
118
|
-
|
|
119
|
-
#### See
|
|
120
|
-
|
|
121
|
-
[`useEventListener`](#useeventlistener)
|
|
122
|
-
|
|
123
|
-
### Call Signature
|
|
124
|
-
|
|
125
|
-
```ts
|
|
126
|
-
function useEventListener<T>(target, eventName, handler, options?): void;
|
|
67
|
+
useEventListener(buttonRef, 'click', () => console.log('click'));
|
|
127
68
|
```
|
|
128
69
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
#### Type Parameters
|
|
132
|
-
|
|
133
|
-
<table>
|
|
134
|
-
<thead>
|
|
135
|
-
<tr>
|
|
136
|
-
<th>Type Parameter</th>
|
|
137
|
-
</tr>
|
|
138
|
-
</thead>
|
|
139
|
-
<tbody>
|
|
140
|
-
<tr>
|
|
141
|
-
<td>
|
|
142
|
-
|
|
143
|
-
`T` _extends_ `EventTarget`
|
|
144
|
-
|
|
145
|
-
</td>
|
|
146
|
-
</tr>
|
|
147
|
-
</tbody>
|
|
148
|
-
</table>
|
|
149
|
-
|
|
150
|
-
#### Parameters
|
|
151
|
-
|
|
152
|
-
<table>
|
|
153
|
-
<thead>
|
|
154
|
-
<tr>
|
|
155
|
-
<th>Parameter</th>
|
|
156
|
-
<th>Type</th>
|
|
157
|
-
</tr>
|
|
158
|
-
</thead>
|
|
159
|
-
<tbody>
|
|
160
|
-
<tr>
|
|
161
|
-
<td>
|
|
162
|
-
|
|
163
|
-
`target`
|
|
164
|
-
|
|
165
|
-
</td>
|
|
166
|
-
<td>
|
|
167
|
-
|
|
168
|
-
`null` \| `T`
|
|
169
|
-
|
|
170
|
-
</td>
|
|
171
|
-
</tr>
|
|
172
|
-
<tr>
|
|
173
|
-
<td>
|
|
174
|
-
|
|
175
|
-
`eventName`
|
|
176
|
-
|
|
177
|
-
</td>
|
|
178
|
-
<td>
|
|
179
|
-
|
|
180
|
-
`string`
|
|
181
|
-
|
|
182
|
-
</td>
|
|
183
|
-
</tr>
|
|
184
|
-
<tr>
|
|
185
|
-
<td>
|
|
186
|
-
|
|
187
|
-
`handler`
|
|
188
|
-
|
|
189
|
-
</td>
|
|
190
|
-
<td>
|
|
191
|
-
|
|
192
|
-
(`this`, `event`) => `void`
|
|
193
|
-
|
|
194
|
-
</td>
|
|
195
|
-
</tr>
|
|
196
|
-
<tr>
|
|
197
|
-
<td>
|
|
198
|
-
|
|
199
|
-
`options?`
|
|
200
|
-
|
|
201
|
-
</td>
|
|
202
|
-
<td>
|
|
203
|
-
|
|
204
|
-
`boolean` \| `AddEventListenerOptions`
|
|
205
|
-
|
|
206
|
-
</td>
|
|
207
|
-
</tr>
|
|
208
|
-
</tbody>
|
|
209
|
-
</table>
|
|
210
|
-
|
|
211
|
-
#### Returns
|
|
212
|
-
|
|
213
|
-
`void`
|
|
214
|
-
|
|
215
|
-
#### See
|
|
70
|
+
### See
|
|
216
71
|
|
|
217
|
-
[`
|
|
72
|
+
[`UseEventListener`](#useeventlistener)
|
|
218
73
|
|
|
219
74
|
---
|
|
220
75
|
|
|
221
|
-
## useForceUpdate()
|
|
76
|
+
## ~~useForceUpdate()~~
|
|
222
77
|
|
|
223
78
|
```ts
|
|
224
79
|
function useForceUpdate(callback?): [() => void, bigint];
|
|
225
80
|
```
|
|
226
81
|
|
|
227
|
-
Defined in: [hooks/useForceUpdate.ts:
|
|
82
|
+
Defined in: [hooks/useForceUpdate.ts:37](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/hooks/useForceUpdate.ts#L37)
|
|
228
83
|
|
|
229
84
|
Enables you to imperatively trigger re-rendering of components
|
|
230
85
|
|
|
231
86
|
This hook is designed in the most general way possible in order to cover all
|
|
232
87
|
imaginable use cases.
|
|
233
88
|
|
|
234
|
-
###
|
|
89
|
+
### Deprecated
|
|
235
90
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
Here is an example of a scenario where that can make sense:
|
|
240
|
-
|
|
241
|
-
```tsx
|
|
242
|
-
type SensorData = { timestamp: number; value: number };
|
|
243
|
-
const sensorDataRef = useRef<SensorData[]>([]);
|
|
244
|
-
const mostRecentSensorDataTimestampRef = useRef<number>(0);
|
|
245
|
-
|
|
246
|
-
const [forceUpdate, updateCount] = useForceUpdate();
|
|
247
|
-
// Limiting the frequency of forced re-renders with some throttle function:
|
|
248
|
-
const throttledForceUpdateRef = useRef(throttle(forceUpdate));
|
|
249
|
-
|
|
250
|
-
useEffect(() => {
|
|
251
|
-
return sensorDataObservable.subscribe((data: SensorData) => {
|
|
252
|
-
// Imagine new sensor data arrives every 1 millisecond. If we were following
|
|
253
|
-
// React's immutability rules by creating a new array every time, the data
|
|
254
|
-
// that's already there would have to be copied many times before the new
|
|
255
|
-
// data would even get a chance to be reflected in the UI for the first time
|
|
256
|
-
// because it typically takes much longer than 1 millisecond for a new frame
|
|
257
|
-
// to be displayed. To prevent the waste of computational resources, we just
|
|
258
|
-
// mutate the existing array every time instead:
|
|
259
|
-
sensorDataRef.current.push(data);
|
|
260
|
-
if (data.timestamp > mostRecentSensorDataTimestampRef.current) {
|
|
261
|
-
mostRecentSensorDataTimestampRef.current = data.timestamp;
|
|
262
|
-
}
|
|
263
|
-
throttledForceUpdateRef.current();
|
|
264
|
-
});
|
|
265
|
-
}, []);
|
|
266
|
-
|
|
267
|
-
const [timeWindow, setTimeWindow] = useState(1000);
|
|
268
|
-
const selectedSensorData = useMemo(
|
|
269
|
-
() => {
|
|
270
|
-
// Keep this line if you don't want to disable the
|
|
271
|
-
// react-hooks/exhaustive-deps ESLint rule:
|
|
272
|
-
updateCount;
|
|
273
|
-
const threshold = mostRecentSensorDataTimestampRef.current - timeWindow;
|
|
274
|
-
return sensorDataRef.current.filter(
|
|
275
|
-
({ timestamp }) => timestamp >= threshold,
|
|
276
|
-
);
|
|
277
|
-
},
|
|
278
|
-
// sensorDataRef.current always references the same array, so listing it as a
|
|
279
|
-
// dependency is pointless. Instead, updateCount should be used:
|
|
280
|
-
[updateCount, timeWindow],
|
|
281
|
-
);
|
|
282
|
-
```
|
|
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/.
|
|
283
94
|
|
|
284
95
|
### Parameters
|
|
285
96
|
|
|
@@ -344,11 +155,18 @@ function useReducerWithDeps<S, A>(
|
|
|
344
155
|
): [S, ActionDispatch<A>];
|
|
345
156
|
```
|
|
346
157
|
|
|
347
|
-
Defined in: [hooks/useReducerWithDeps.ts:
|
|
158
|
+
Defined in: [hooks/useReducerWithDeps.ts:59](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/hooks/useReducerWithDeps.ts#L59)
|
|
348
159
|
|
|
349
160
|
`useReducer` hook with an additional dependency array `deps` that resets the
|
|
350
161
|
state to `initialState` when dependencies change
|
|
351
162
|
|
|
163
|
+
This hook is the reducer pattern counterpart of [`useStateWithDeps`](#usestatewithdeps).
|
|
164
|
+
|
|
165
|
+
Due to React's limitations, a change in dependencies always causes two
|
|
166
|
+
renders when using this hook. The result of the first render is thrown away
|
|
167
|
+
as described in
|
|
168
|
+
[useState > Storing information from previous renders](https://react.dev/reference/react/useState#storing-information-from-previous-renders).
|
|
169
|
+
|
|
352
170
|
For motivation and examples, see
|
|
353
171
|
https://github.com/facebook/react/issues/33041.
|
|
354
172
|
|
|
@@ -480,11 +298,16 @@ function useStateWithDeps<S>(
|
|
|
480
298
|
): [S, Dispatch<SetStateAction<S>>];
|
|
481
299
|
```
|
|
482
300
|
|
|
483
|
-
Defined in: [hooks/useStateWithDeps.ts:
|
|
301
|
+
Defined in: [hooks/useStateWithDeps.ts:62](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/hooks/useStateWithDeps.ts#L62)
|
|
484
302
|
|
|
485
303
|
`useState` hook with an additional dependency array `deps` that resets the
|
|
486
304
|
state to `initialState` when dependencies change
|
|
487
305
|
|
|
306
|
+
Due to React's limitations, a change in dependencies always causes two
|
|
307
|
+
renders when using this hook. The result of the first render is thrown away
|
|
308
|
+
as described in
|
|
309
|
+
[useState > Storing information from previous renders](https://react.dev/reference/react/useState#storing-information-from-previous-renders).
|
|
310
|
+
|
|
488
311
|
For motivation and more examples, see
|
|
489
312
|
https://github.com/facebook/react/issues/33041.
|
|
490
313
|
|
|
@@ -504,7 +327,7 @@ const activityOptionsByTimeOfDay: {
|
|
|
504
327
|
evening: ['board games', 'dinner'],
|
|
505
328
|
};
|
|
506
329
|
|
|
507
|
-
|
|
330
|
+
function Example() {
|
|
508
331
|
const [timeOfDay, setTimeOfDay] = useState<TimeOfDay>('morning');
|
|
509
332
|
|
|
510
333
|
const activityOptions = activityOptionsByTimeOfDay[timeOfDay];
|
|
@@ -607,7 +430,7 @@ function createSafeContext<T>(): <DisplayName>(
|
|
|
607
430
|
) => SafeContext<DisplayName, T>;
|
|
608
431
|
```
|
|
609
432
|
|
|
610
|
-
Defined in: [misc/createSafeContext.ts:
|
|
433
|
+
Defined in: [misc/createSafeContext.ts:95](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/misc/createSafeContext.ts#L95)
|
|
611
434
|
|
|
612
435
|
For a given type `T`, returns a function that produces both a context of that
|
|
613
436
|
type and a hook that returns the current context value if one was provided,
|
|
@@ -744,6 +567,337 @@ A function that accepts a single string argument `displayName` (e.g.
|
|
|
744
567
|
|
|
745
568
|
[`SafeContext`](#safecontext)\<`DisplayName`, `T`\>
|
|
746
569
|
|
|
570
|
+
### See
|
|
571
|
+
|
|
572
|
+
[`SafeContext`](#safecontext)
|
|
573
|
+
|
|
574
|
+
---
|
|
575
|
+
|
|
576
|
+
## UseEventListener
|
|
577
|
+
|
|
578
|
+
```ts
|
|
579
|
+
type UseEventListener = UseEventListenerWithImplicitWindowTarget &
|
|
580
|
+
UseEventListenerWithExplicitGlobalTarget &
|
|
581
|
+
UseEventListenerWithAnyExplicitTarget;
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
Defined in: [hooks/useEventListener.ts:12](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/hooks/useEventListener.ts#L12)
|
|
585
|
+
|
|
586
|
+
The type of [`useEventListener`](#useeventlistener-1)
|
|
587
|
+
|
|
588
|
+
### See
|
|
589
|
+
|
|
590
|
+
[`useEventListener`](#useeventlistener-1),
|
|
591
|
+
[`UseEventListenerWithImplicitWindowTarget`](#useeventlistenerwithimplicitwindowtarget),
|
|
592
|
+
[`UseEventListenerWithExplicitGlobalTarget`](#useeventlistenerwithexplicitglobaltarget),
|
|
593
|
+
[`UseEventListenerWithAnyExplicitTarget`](#useeventlistenerwithanyexplicittarget)
|
|
594
|
+
|
|
595
|
+
---
|
|
596
|
+
|
|
597
|
+
## UseEventListenerWithImplicitWindowTarget()
|
|
598
|
+
|
|
599
|
+
```ts
|
|
600
|
+
type UseEventListenerWithImplicitWindowTarget = <K>(...args) => void;
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
Defined in: [hooks/useEventListener.ts:21](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/hooks/useEventListener.ts#L21)
|
|
604
|
+
|
|
605
|
+
### Type Parameters
|
|
606
|
+
|
|
607
|
+
<table>
|
|
608
|
+
<thead>
|
|
609
|
+
<tr>
|
|
610
|
+
<th>Type Parameter</th>
|
|
611
|
+
</tr>
|
|
612
|
+
</thead>
|
|
613
|
+
<tbody>
|
|
614
|
+
<tr>
|
|
615
|
+
<td>
|
|
616
|
+
|
|
617
|
+
`K` _extends_ keyof `WindowEventMap`
|
|
618
|
+
|
|
619
|
+
</td>
|
|
620
|
+
</tr>
|
|
621
|
+
</tbody>
|
|
622
|
+
</table>
|
|
623
|
+
|
|
624
|
+
### Parameters
|
|
625
|
+
|
|
626
|
+
<table>
|
|
627
|
+
<thead>
|
|
628
|
+
<tr>
|
|
629
|
+
<th>Parameter</th>
|
|
630
|
+
<th>Type</th>
|
|
631
|
+
</tr>
|
|
632
|
+
</thead>
|
|
633
|
+
<tbody>
|
|
634
|
+
<tr>
|
|
635
|
+
<td>
|
|
636
|
+
|
|
637
|
+
...`args`
|
|
638
|
+
|
|
639
|
+
</td>
|
|
640
|
+
<td>
|
|
641
|
+
|
|
642
|
+
[`UseEventListenerWithImplicitWindowTargetArgs`](#useeventlistenerwithimplicitwindowtargetargs)\<`K`\>
|
|
643
|
+
|
|
644
|
+
</td>
|
|
645
|
+
</tr>
|
|
646
|
+
</tbody>
|
|
647
|
+
</table>
|
|
648
|
+
|
|
649
|
+
### Returns
|
|
650
|
+
|
|
651
|
+
`void`
|
|
652
|
+
|
|
653
|
+
### See
|
|
654
|
+
|
|
655
|
+
[`useEventListener`](#useeventlistener-1),
|
|
656
|
+
[`UseEventListenerWithImplicitWindowTargetArgs`](#useeventlistenerwithimplicitwindowtargetargs)
|
|
657
|
+
|
|
658
|
+
---
|
|
659
|
+
|
|
660
|
+
## UseEventListenerWithExplicitGlobalTarget
|
|
661
|
+
|
|
662
|
+
```ts
|
|
663
|
+
type UseEventListenerWithExplicitGlobalTarget =
|
|
664
|
+
UseEventListenerWithExplicitTarget<Window, WindowEventMap> &
|
|
665
|
+
UseEventListenerWithExplicitTarget<Document, DocumentEventMap> &
|
|
666
|
+
UseEventListenerWithExplicitTarget<HTMLElement, HTMLElementEventMap> &
|
|
667
|
+
UseEventListenerWithExplicitTarget<SVGElement, SVGElementEventMap> &
|
|
668
|
+
UseEventListenerWithExplicitTarget<MathMLElement, MathMLElementEventMap>;
|
|
669
|
+
```
|
|
670
|
+
|
|
671
|
+
Defined in: [hooks/useEventListener.ts:32](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/hooks/useEventListener.ts#L32)
|
|
672
|
+
|
|
673
|
+
### See
|
|
674
|
+
|
|
675
|
+
[`useEventListener`](#useeventlistener-1),
|
|
676
|
+
[`UseEventListenerWithExplicitTarget`](#useeventlistenerwithexplicittarget)
|
|
677
|
+
|
|
678
|
+
---
|
|
679
|
+
|
|
680
|
+
## UseEventListenerWithExplicitTarget()
|
|
681
|
+
|
|
682
|
+
```ts
|
|
683
|
+
type UseEventListenerWithExplicitTarget<Target, EventMap> = <T, K>(
|
|
684
|
+
...args
|
|
685
|
+
) => void;
|
|
686
|
+
```
|
|
687
|
+
|
|
688
|
+
Defined in: [hooks/useEventListener.ts:44](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/hooks/useEventListener.ts#L44)
|
|
689
|
+
|
|
690
|
+
### Type Parameters
|
|
691
|
+
|
|
692
|
+
<table>
|
|
693
|
+
<thead>
|
|
694
|
+
<tr>
|
|
695
|
+
<th>Type Parameter</th>
|
|
696
|
+
<th>Default type</th>
|
|
697
|
+
</tr>
|
|
698
|
+
</thead>
|
|
699
|
+
<tbody>
|
|
700
|
+
<tr>
|
|
701
|
+
<td>
|
|
702
|
+
|
|
703
|
+
`Target` _extends_ `EventTarget`
|
|
704
|
+
|
|
705
|
+
</td>
|
|
706
|
+
<td>
|
|
707
|
+
|
|
708
|
+
‐
|
|
709
|
+
|
|
710
|
+
</td>
|
|
711
|
+
</tr>
|
|
712
|
+
<tr>
|
|
713
|
+
<td>
|
|
714
|
+
|
|
715
|
+
`EventMap`
|
|
716
|
+
|
|
717
|
+
</td>
|
|
718
|
+
<td>
|
|
719
|
+
|
|
720
|
+
`Record`\<`string`, `Event`\>
|
|
721
|
+
|
|
722
|
+
</td>
|
|
723
|
+
</tr>
|
|
724
|
+
</tbody>
|
|
725
|
+
</table>
|
|
726
|
+
|
|
727
|
+
### Type Parameters
|
|
728
|
+
|
|
729
|
+
<table>
|
|
730
|
+
<thead>
|
|
731
|
+
<tr>
|
|
732
|
+
<th>Type Parameter</th>
|
|
733
|
+
</tr>
|
|
734
|
+
</thead>
|
|
735
|
+
<tbody>
|
|
736
|
+
<tr>
|
|
737
|
+
<td>
|
|
738
|
+
|
|
739
|
+
`T` _extends_ `Target`
|
|
740
|
+
|
|
741
|
+
</td>
|
|
742
|
+
</tr>
|
|
743
|
+
<tr>
|
|
744
|
+
<td>
|
|
745
|
+
|
|
746
|
+
`K` _extends_ keyof `EventMap`
|
|
747
|
+
|
|
748
|
+
</td>
|
|
749
|
+
</tr>
|
|
750
|
+
</tbody>
|
|
751
|
+
</table>
|
|
752
|
+
|
|
753
|
+
### Parameters
|
|
754
|
+
|
|
755
|
+
<table>
|
|
756
|
+
<thead>
|
|
757
|
+
<tr>
|
|
758
|
+
<th>Parameter</th>
|
|
759
|
+
<th>Type</th>
|
|
760
|
+
</tr>
|
|
761
|
+
</thead>
|
|
762
|
+
<tbody>
|
|
763
|
+
<tr>
|
|
764
|
+
<td>
|
|
765
|
+
|
|
766
|
+
...`args`
|
|
767
|
+
|
|
768
|
+
</td>
|
|
769
|
+
<td>
|
|
770
|
+
|
|
771
|
+
[`UseEventListenerWithExplicitTargetArgs`](#useeventlistenerwithexplicittargetargs)\<`EventMap`, `T`, `K`\>
|
|
772
|
+
|
|
773
|
+
</td>
|
|
774
|
+
</tr>
|
|
775
|
+
</tbody>
|
|
776
|
+
</table>
|
|
777
|
+
|
|
778
|
+
### Returns
|
|
779
|
+
|
|
780
|
+
`void`
|
|
781
|
+
|
|
782
|
+
### See
|
|
783
|
+
|
|
784
|
+
[`useEventListener`](#useeventlistener-1),
|
|
785
|
+
[`UseEventListenerWithExplicitTargetArgs`](#useeventlistenerwithexplicittargetargs)
|
|
786
|
+
|
|
787
|
+
---
|
|
788
|
+
|
|
789
|
+
## UseEventListenerWithAnyExplicitTarget
|
|
790
|
+
|
|
791
|
+
```ts
|
|
792
|
+
type UseEventListenerWithAnyExplicitTarget =
|
|
793
|
+
UseEventListenerWithExplicitTarget<EventTarget>;
|
|
794
|
+
```
|
|
795
|
+
|
|
796
|
+
Defined in: [hooks/useEventListener.ts:56](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/hooks/useEventListener.ts#L56)
|
|
797
|
+
|
|
798
|
+
### See
|
|
799
|
+
|
|
800
|
+
[`useEventListener`](#useeventlistener-1),
|
|
801
|
+
[`UseEventListenerWithExplicitTarget`](#useeventlistenerwithexplicittarget)
|
|
802
|
+
|
|
803
|
+
---
|
|
804
|
+
|
|
805
|
+
## UseEventListenerWithImplicitWindowTargetArgs
|
|
806
|
+
|
|
807
|
+
```ts
|
|
808
|
+
type UseEventListenerWithImplicitWindowTargetArgs<K> =
|
|
809
|
+
UseEventListenerWithExplicitTargetArgs<WindowEventMap, Window, K> extends [
|
|
810
|
+
unknown,
|
|
811
|
+
...infer Args,
|
|
812
|
+
]
|
|
813
|
+
? Args
|
|
814
|
+
: never;
|
|
815
|
+
```
|
|
816
|
+
|
|
817
|
+
Defined in: [hooks/useEventListener.ts:64](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/hooks/useEventListener.ts#L64)
|
|
818
|
+
|
|
819
|
+
### Type Parameters
|
|
820
|
+
|
|
821
|
+
<table>
|
|
822
|
+
<thead>
|
|
823
|
+
<tr>
|
|
824
|
+
<th>Type Parameter</th>
|
|
825
|
+
</tr>
|
|
826
|
+
</thead>
|
|
827
|
+
<tbody>
|
|
828
|
+
<tr>
|
|
829
|
+
<td>
|
|
830
|
+
|
|
831
|
+
`K` _extends_ keyof `WindowEventMap`
|
|
832
|
+
|
|
833
|
+
</td>
|
|
834
|
+
</tr>
|
|
835
|
+
</tbody>
|
|
836
|
+
</table>
|
|
837
|
+
|
|
838
|
+
### See
|
|
839
|
+
|
|
840
|
+
[`useEventListener`](#useeventlistener-1),
|
|
841
|
+
[`UseEventListenerWithExplicitTargetArgs`](#useeventlistenerwithexplicittargetargs)
|
|
842
|
+
|
|
843
|
+
---
|
|
844
|
+
|
|
845
|
+
## UseEventListenerWithExplicitTargetArgs
|
|
846
|
+
|
|
847
|
+
```ts
|
|
848
|
+
type UseEventListenerWithExplicitTargetArgs<EventMap, T, K> = [
|
|
849
|
+
(
|
|
850
|
+
| T
|
|
851
|
+
| (RefObject<T> & {
|
|
852
|
+
addEventListener?: never;
|
|
853
|
+
})
|
|
854
|
+
| null
|
|
855
|
+
),
|
|
856
|
+
K,
|
|
857
|
+
(this, event) => void,
|
|
858
|
+
AddEventListenerOptions | boolean | undefined,
|
|
859
|
+
];
|
|
860
|
+
```
|
|
861
|
+
|
|
862
|
+
Defined in: [hooks/useEventListener.ts:78](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/hooks/useEventListener.ts#L78)
|
|
863
|
+
|
|
864
|
+
### Type Parameters
|
|
865
|
+
|
|
866
|
+
<table>
|
|
867
|
+
<thead>
|
|
868
|
+
<tr>
|
|
869
|
+
<th>Type Parameter</th>
|
|
870
|
+
</tr>
|
|
871
|
+
</thead>
|
|
872
|
+
<tbody>
|
|
873
|
+
<tr>
|
|
874
|
+
<td>
|
|
875
|
+
|
|
876
|
+
`EventMap`
|
|
877
|
+
|
|
878
|
+
</td>
|
|
879
|
+
</tr>
|
|
880
|
+
<tr>
|
|
881
|
+
<td>
|
|
882
|
+
|
|
883
|
+
`T` _extends_ `EventTarget`
|
|
884
|
+
|
|
885
|
+
</td>
|
|
886
|
+
</tr>
|
|
887
|
+
<tr>
|
|
888
|
+
<td>
|
|
889
|
+
|
|
890
|
+
`K` _extends_ keyof `EventMap`
|
|
891
|
+
|
|
892
|
+
</td>
|
|
893
|
+
</tr>
|
|
894
|
+
</tbody>
|
|
895
|
+
</table>
|
|
896
|
+
|
|
897
|
+
### See
|
|
898
|
+
|
|
899
|
+
[`useEventListener`](#useeventlistener-1)
|
|
900
|
+
|
|
747
901
|
---
|
|
748
902
|
|
|
749
903
|
## RestrictedContext
|
|
@@ -761,7 +915,7 @@ type RestrictedContext<T> =
|
|
|
761
915
|
};
|
|
762
916
|
```
|
|
763
917
|
|
|
764
|
-
Defined in: [misc/createSafeContext.ts:
|
|
918
|
+
Defined in: [misc/createSafeContext.ts:18](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/misc/createSafeContext.ts#L18)
|
|
765
919
|
|
|
766
920
|
A React context with a required `displayName` and the obsolete `Consumer`
|
|
767
921
|
property purposefully omitted so that it is impossible to pass the context
|
|
@@ -801,7 +955,7 @@ type SafeContext<DisplayName, T> = {
|
|
|
801
955
|
} & { [K in `use${DisplayName}`]: () => T };
|
|
802
956
|
```
|
|
803
957
|
|
|
804
|
-
Defined in: [misc/createSafeContext.ts:
|
|
958
|
+
Defined in: [misc/createSafeContext.ts:30](https://github.com/aweebit/react-essentials/blob/v0.8.1/src/misc/createSafeContext.ts#L30)
|
|
805
959
|
|
|
806
960
|
The return type of [`createSafeContext`](#createsafecontext)
|
|
807
961
|
|
|
@@ -833,4 +987,5 @@ The return type of [`createSafeContext`](#createsafecontext)
|
|
|
833
987
|
|
|
834
988
|
### See
|
|
835
989
|
|
|
836
|
-
[`createSafeContext`](#createsafecontext)
|
|
990
|
+
[`createSafeContext`](#createsafecontext),
|
|
991
|
+
[`RestrictedContext`](#restrictedcontext)
|