@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.
@@ -1,19 +1,72 @@
1
+ import { type RefObject } from 'react';
1
2
  /**
2
- * @file Based on {@link https://github.com/juliencrn/usehooks-ts}
3
+ * The type of {@linkcode useEventListener}
3
4
  *
4
- * @license MIT
5
- * @copyright 2020 Julien CARON
5
+ * @see
6
+ * {@linkcode useEventListener},
7
+ * {@linkcode UseEventListenerWithImplicitWindowTarget},
8
+ * {@linkcode UseEventListenerWithExplicitGlobalTarget},
9
+ * {@linkcode UseEventListenerWithAnyExplicitTarget}
6
10
  */
7
- type UseEventListenerOverloadArgs<EventMap, K extends keyof EventMap, T extends EventTarget> = [
8
- target: T | null,
11
+ export type UseEventListener = UseEventListenerWithImplicitWindowTarget & UseEventListenerWithExplicitGlobalTarget & UseEventListenerWithAnyExplicitTarget;
12
+ /**
13
+ * @see
14
+ * {@linkcode useEventListener},
15
+ * {@linkcode UseEventListenerWithImplicitWindowTargetArgs}
16
+ */
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>;
24
+ /**
25
+ * @see
26
+ * {@linkcode useEventListener},
27
+ * {@linkcode UseEventListenerWithExplicitTargetArgs}
28
+ */
29
+ export type UseEventListenerWithExplicitTarget<Target extends EventTarget, EventMap = Record<string, Event>> = <T extends Target, K extends keyof EventMap>(...args: UseEventListenerWithExplicitTargetArgs<EventMap, T, K>) => void;
30
+ /**
31
+ * @see
32
+ * {@linkcode useEventListener},
33
+ * {@linkcode UseEventListenerWithExplicitTarget}
34
+ */
35
+ export type UseEventListenerWithAnyExplicitTarget = UseEventListenerWithExplicitTarget<EventTarget>;
36
+ /**
37
+ * @see
38
+ * {@linkcode useEventListener},
39
+ * {@linkcode UseEventListenerWithExplicitTargetArgs}
40
+ */
41
+ export type UseEventListenerWithImplicitWindowTargetArgs<K extends keyof WindowEventMap> = UseEventListenerWithExplicitTargetArgs<WindowEventMap, Window, K> extends [
42
+ unknown,
43
+ ...infer Args
44
+ ] ? Args : never;
45
+ /**
46
+ * @see
47
+ * {@linkcode useEventListener}
48
+ */
49
+ export type UseEventListenerWithExplicitTargetArgs<EventMap, T extends EventTarget, K extends keyof EventMap> = [
50
+ target: T | (RefObject<T> & {
51
+ addEventListener?: never;
52
+ }) | null,
9
53
  eventName: K,
10
54
  handler: (this: NoInfer<T>, event: EventMap[K]) => void,
11
- options?: AddEventListenerOptions | boolean
55
+ options?: AddEventListenerOptions | boolean | undefined
12
56
  ];
13
57
  /**
14
58
  * Adds `handler` as a listener for the event `eventName` of `target` with the
15
59
  * provided `options` applied
16
60
  *
61
+ * The following call signatures are available:
62
+ *
63
+ * ```ts
64
+ * function useEventListener(eventName, handler, options?): void;
65
+ * function useEventListener(target, eventName, handler, options?): void;
66
+ * ```
67
+ *
68
+ * For the full definition of the hook's type, see {@linkcode UseEventListener}.
69
+ *
17
70
  * If `target` is not provided, `window` is used instead.
18
71
  *
19
72
  * If `target` is `null`, no event listener is added. This is useful when
@@ -31,44 +84,11 @@ type UseEventListenerOverloadArgs<EventMap, K extends keyof EventMap, T extends
31
84
  * });
32
85
  *
33
86
  * const buttonRef = useRef<HTMLButtonElement>(null);
34
- * useEventListener(buttonRef.current, 'click', () => console.log('click'));
87
+ * useEventListener(buttonRef, 'click', () => console.log('click'));
35
88
  * ```
36
89
  *
37
- * @ignore
38
- */
39
- export declare function useEventListener<K extends keyof WindowEventMap>(eventName: K, handler: (this: Window, event: WindowEventMap[K]) => void, options?: AddEventListenerOptions | boolean): void;
40
- /**
41
- * @see {@linkcode useEventListener}
42
- * @ignore
43
- */
44
- export declare function useEventListener<K extends keyof WindowEventMap>(...args: UseEventListenerOverloadArgs<WindowEventMap, K, Window>): void;
45
- /**
46
- * @see {@linkcode useEventListener}
47
- * @ignore
48
- */
49
- export declare function useEventListener<K extends keyof DocumentEventMap>(...args: UseEventListenerOverloadArgs<DocumentEventMap, K, Document>): void;
50
- /**
51
- * @see {@linkcode useEventListener}
52
- * @ignore
53
- */
54
- export declare function useEventListener<K extends keyof HTMLElementEventMap, T extends HTMLElement>(...args: UseEventListenerOverloadArgs<HTMLElementEventMap, K, T>): void;
55
- /**
56
- * @see {@linkcode useEventListener}
57
- * @ignore
58
- */
59
- export declare function useEventListener<K extends keyof SVGElementEventMap, T extends SVGElement>(...args: UseEventListenerOverloadArgs<SVGElementEventMap, K, T>): void;
60
- /**
61
- * @see {@linkcode useEventListener}
62
- * @ignore
63
- */
64
- export declare function useEventListener<K extends keyof MathMLElementEventMap, T extends MathMLElement>(...args: UseEventListenerOverloadArgs<MathMLElementEventMap, K, T>): void;
65
- /**
66
- * @see {@linkcode useEventListener}
67
- */
68
- export declare function useEventListener<K extends keyof WindowEventMap>(eventName: K, handler: (this: Window, event: WindowEventMap[K]) => void, options?: AddEventListenerOptions | boolean): void;
69
- /**
70
- * @see {@linkcode useEventListener}
90
+ * @see
91
+ * {@linkcode UseEventListener}
71
92
  */
72
- export declare function useEventListener<T extends EventTarget>(target: T | null, eventName: string, handler: (this: NoInfer<T>, event: Event) => void, options?: AddEventListenerOptions | boolean): void;
73
- export {};
93
+ export declare const useEventListener: UseEventListener;
74
94
  //# sourceMappingURL=useEventListener.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useEventListener.d.ts","sourceRoot":"","sources":["../../src/hooks/useEventListener.ts"],"names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uB,GAAG,OAAO,GAAG,SAAS;CACxD,CAAC;AAYF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,eAAO,MAAM,gBAAgB,EAAE,gBAoDV,CAAC"}
@@ -1,14 +1,17 @@
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
10
4
  * provided `options` applied
11
5
  *
6
+ * The following call signatures are available:
7
+ *
8
+ * ```ts
9
+ * function useEventListener(eventName, handler, options?): void;
10
+ * function useEventListener(target, eventName, handler, options?): void;
11
+ * ```
12
+ *
13
+ * For the full definition of the hook's type, see {@linkcode UseEventListener}.
14
+ *
12
15
  * If `target` is not provided, `window` is used instead.
13
16
  *
14
17
  * If `target` is `null`, no event listener is added. This is useful when
@@ -26,40 +29,37 @@ import { useEffect, useMemo, useRef } from 'react';
26
29
  * });
27
30
  *
28
31
  * const buttonRef = useRef<HTMLButtonElement>(null);
29
- * useEventListener(buttonRef.current, 'click', () => console.log('click'));
32
+ * useEventListener(buttonRef, 'click', () => console.log('click'));
30
33
  * ```
34
+ *
35
+ * @see
36
+ * {@linkcode UseEventListener}
31
37
  */
32
- export function useEventListener(...args) {
33
- let eventName;
34
- let handler;
35
- let target;
36
- let options;
37
- if (typeof args[0] === 'string') {
38
- [eventName, handler, options] = args;
39
- }
40
- else {
41
- [target, eventName, handler, options] =
42
- args;
43
- }
38
+ export const useEventListener = function useEventListener(...args) {
39
+ const [target, eventName, handler, options] = typeof args[0] === 'string'
40
+ ? [window, ...args]
41
+ : args;
42
+ const unwrappedTarget = target && !('addEventListener' in target) ? target.current : target;
44
43
  const handlerRef = useRef(handler);
45
- handlerRef.current = handler;
44
+ useEffect(() => {
45
+ handlerRef.current = handler;
46
+ }, [handler]);
46
47
  const { capture = false, once = false, passive, signal, } = typeof options === 'boolean' ? { capture: options } : (options ?? {});
47
48
  const memoizedOptions = useMemo(() => options,
48
49
  // eslint-disable-next-line react-hooks/exhaustive-deps
49
50
  [capture, once, passive, signal]);
50
51
  useEffect(() => {
51
- if (target === null) {
52
+ if (unwrappedTarget === null) {
52
53
  // No element has been attached to the ref yet
53
54
  return;
54
55
  }
55
- const definedTarget = target ?? window;
56
56
  const listener = function (event) {
57
57
  handlerRef.current.call(this, event);
58
58
  };
59
- definedTarget.addEventListener(eventName, listener, memoizedOptions);
59
+ unwrappedTarget.addEventListener(eventName, listener, memoizedOptions);
60
60
  return () => {
61
- definedTarget.removeEventListener(eventName, listener, memoizedOptions);
61
+ unwrappedTarget.removeEventListener(eventName, listener, memoizedOptions);
62
62
  };
63
- }, [eventName, target, memoizedOptions]);
64
- }
63
+ }, [unwrappedTarget, eventName, memoizedOptions]);
64
+ };
65
65
  //# sourceMappingURL=useEventListener.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useEventListener.js","sourceRoot":"","sources":["../../src/hooks/useEventListener.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AA2GnD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,gBAAgB,CAC9B,GAAG,IAAwE;IAE3E,IAAI,SAAiB,CAAC;IACtB,IAAI,OAAkD,CAAC;IACvD,IAAI,MAAsC,CAAC;IAC3C,IAAI,OAAsD,CAAC;IAE3D,IAAI,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,QAAQ,EAAE,CAAC;QAChC,CAAC,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,GAAG,IAAyC,CAAC;IAC5E,CAAC;SAAM,CAAC;QACN,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC;YACnC,IAAsC,CAAC;IAC3C,CAAC;IAED,MAAM,UAAU,GAAG,MAAM,C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AE,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"}
@@ -4,54 +4,10 @@
4
4
  * This hook is designed in the most general way possible in order to cover all
5
5
  * imaginable use cases.
6
6
  *
7
- * @example
8
- * Sometimes, React's immutability constraints mean too much unnecessary copying
9
- * of data when new data arrives at a high frequency. In such cases, it might be
10
- * desirable to ignore the constraints by embracing imperative patterns.
11
- * Here is an example of a scenario where that can make sense:
12
- *
13
- * ```tsx
14
- * type SensorData = { timestamp: number; value: number };
15
- * const sensorDataRef = useRef<SensorData[]>([]);
16
- * const mostRecentSensorDataTimestampRef = useRef<number>(0);
17
- *
18
- * const [forceUpdate, updateCount] = useForceUpdate();
19
- * // Limiting the frequency of forced re-renders with some throttle function:
20
- * const throttledForceUpdateRef = useRef(throttle(forceUpdate));
21
- *
22
- * useEffect(() => {
23
- * return sensorDataObservable.subscribe((data: SensorData) => {
24
- * // Imagine new sensor data arrives every 1 millisecond. If we were following
25
- * // React's immutability rules by creating a new array every time, the data
26
- * // that's already there would have to be copied many times before the new
27
- * // data would even get a chance to be reflected in the UI for the first time
28
- * // because it typically takes much longer than 1 millisecond for a new frame
29
- * // to be displayed. To prevent the waste of computational resources, we just
30
- * // mutate the existing array every time instead:
31
- * sensorDataRef.current.push(data);
32
- * if (data.timestamp > mostRecentSensorDataTimestampRef.current) {
33
- * mostRecentSensorDataTimestampRef.current = data.timestamp;
34
- * }
35
- * throttledForceUpdateRef.current();
36
- * });
37
- * }, []);
38
- *
39
- * const [timeWindow, setTimeWindow] = useState(1000);
40
- * const selectedSensorData = useMemo(
41
- * () => {
42
- * // Keep this line if you don't want to disable the
43
- * // react-hooks/exhaustive-deps ESLint rule:
44
- * updateCount;
45
- * const threshold = mostRecentSensorDataTimestampRef.current - timeWindow;
46
- * return sensorDataRef.current.filter(
47
- * ({ timestamp }) => timestamp >= threshold,
48
- * );
49
- * },
50
- * // sensorDataRef.current always references the same array, so listing it as a
51
- * // dependency is pointless. Instead, updateCount should be used:
52
- * [updateCount, timeWindow],
53
- * );
54
- * ```
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/.
55
11
  *
56
12
  * @param callback An optional callback function to call during renders that
57
13
  * were triggered with `forceUpdate()`
@@ -1 +1 @@
1
- {"version":3,"file":"useForceUpdate.d.ts","sourceRoot":"","sources":["../../src/hooks/useForceUpdate.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwEG;AACH,wBAAgB,cAAc,CAAC,QAAQ,CAAC,EAAE,MAAM,IAAI,GAAG,CAAC,MAAM,IAAI,EAAE,MAAM,CAAC,CAU1E"}
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"}
@@ -6,54 +6,10 @@ import { useReducer, useRef } from 'react';
6
6
  * This hook is designed in the most general way possible in order to cover all
7
7
  * imaginable use cases.
8
8
  *
9
- * @example
10
- * Sometimes, React's immutability constraints mean too much unnecessary copying
11
- * of data when new data arrives at a high frequency. In such cases, it might be
12
- * desirable to ignore the constraints by embracing imperative patterns.
13
- * Here is an example of a scenario where that can make sense:
14
- *
15
- * ```tsx
16
- * type SensorData = { timestamp: number; value: number };
17
- * const sensorDataRef = useRef<SensorData[]>([]);
18
- * const mostRecentSensorDataTimestampRef = useRef<number>(0);
19
- *
20
- * const [forceUpdate, updateCount] = useForceUpdate();
21
- * // Limiting the frequency of forced re-renders with some throttle function:
22
- * const throttledForceUpdateRef = useRef(throttle(forceUpdate));
23
- *
24
- * useEffect(() => {
25
- * return sensorDataObservable.subscribe((data: SensorData) => {
26
- * // Imagine new sensor data arrives every 1 millisecond. If we were following
27
- * // React's immutability rules by creating a new array every time, the data
28
- * // that's already there would have to be copied many times before the new
29
- * // data would even get a chance to be reflected in the UI for the first time
30
- * // because it typically takes much longer than 1 millisecond for a new frame
31
- * // to be displayed. To prevent the waste of computational resources, we just
32
- * // mutate the existing array every time instead:
33
- * sensorDataRef.current.push(data);
34
- * if (data.timestamp > mostRecentSensorDataTimestampRef.current) {
35
- * mostRecentSensorDataTimestampRef.current = data.timestamp;
36
- * }
37
- * throttledForceUpdateRef.current();
38
- * });
39
- * }, []);
40
- *
41
- * const [timeWindow, setTimeWindow] = useState(1000);
42
- * const selectedSensorData = useMemo(
43
- * () => {
44
- * // Keep this line if you don't want to disable the
45
- * // react-hooks/exhaustive-deps ESLint rule:
46
- * updateCount;
47
- * const threshold = mostRecentSensorDataTimestampRef.current - timeWindow;
48
- * return sensorDataRef.current.filter(
49
- * ({ timestamp }) => timestamp >= threshold,
50
- * );
51
- * },
52
- * // sensorDataRef.current always references the same array, so listing it as a
53
- * // dependency is pointless. Instead, updateCount should be used:
54
- * [updateCount, timeWindow],
55
- * );
56
- * ```
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/.
57
13
  *
58
14
  * @param callback An optional callback function to call during renders that
59
15
  * were triggered with `forceUpdate()`
@@ -1 +1 @@
1
- {"version":3,"file":"useForceUpdate.js","sourceRoot":"","sources":["../../src/hooks/useForceUpdate.ts"],"names":[],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CAAC;IACD,OAAO,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;AACpC,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,EAAuB,MAAM,OAAO,CAAC;AAOjE,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;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"}
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"}
@@ -1,9 +1,16 @@
1
- import { useCallback, useRef } from 'react';
1
+ import { useRef } from 'react';
2
2
  import { useStateWithDeps } from './useStateWithDeps.js';
3
3
  /**
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
- const dispatch = useCallback(function dispatch(...args) {
50
+ function dispatch(...args) {
44
51
  setState((previousState) => reducerRef.current(previousState, ...args));
45
- }, []); // eslint-disable-line react-hooks/exhaustive-deps
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,WAAW,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAezD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;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,MAAM,QAAQ,GAAG,WAAW,CAAC,SAAS,QAAQ,CAAC,GAAG,IAAO;QACvD,QAAQ,CAAC,CAAC,aAAa,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,aAAa,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;IAC1E,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,kDAAkD;IAE1D,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;AAC3B,CAAC"}
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
- * export function Example() {
29
+ * function Example() {
31
30
  * const [timeOfDay, setTimeOfDay] = useState<TimeOfDay>('morning');
32
31
  *
33
32
  * const activityOptions = activityOptionsByTimeOfDay[timeOfDay];
@@ -1 +1 @@
1
- {"version":3,"file":"useStateWithDeps.d.ts","sourceRoot":"","sources":["../../src/hooks/useStateWithDeps.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAGL,KAAK,cAAc,EACnB,KAAK,QAAQ,EACb,KAAK,cAAc,EACpB,MAAM,OAAO,CAAC;AAIf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAChC,YAAY,EAAE,CAAC,GAAG,CAAC,CAAC,aAAa,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,EAC5C,IAAI,EAAE,cAAc,GACnB,CAAC,CAAC,EAAE,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CA0ClC"}
1
+ {"version":3,"file":"useStateWithDeps.d.ts","sourceRoot":"","sources":["../../src/hooks/useStateWithDeps.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,QAAQ,EACb,KAAK,cAAc,EACpB,MAAM,OAAO,CAAC;AAGf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAChC,YAAY,EAAE,CAAC,GAAG,CAAC,CAAC,aAAa,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,EAC5C,IAAI,EAAE,cAAc,GACnB,CAAC,CAAC,EAAE,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAWlC"}
@@ -1,16 +1,14 @@
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
- import { useCallback, useRef, } from 'react';
8
- import { depsAreEqual, isFunction } from '../utils.js';
9
- import { useForceUpdate } from './useForceUpdate.js';
1
+ import { useState, } from 'react';
2
+ import { depsAreEqual } from '../utils.js';
10
3
  /**
11
4
  * `useState` hook with an additional dependency array `deps` that resets the
12
5
  * state to `initialState` when dependencies change
13
6
  *
7
+ * Due to React's limitations, a change in dependencies always causes two
8
+ * renders when using this hook. The result of the first render is thrown away
9
+ * as described in
10
+ * [useState > Storing information from previous renders](https://react.dev/reference/react/useState#storing-information-from-previous-renders).
11
+ *
14
12
  * For motivation and more examples, see
15
13
  * https://github.com/facebook/react/issues/33041.
16
14
  *
@@ -29,7 +27,7 @@ import { useForceUpdate } from './useForceUpdate.js';
29
27
  * evening: ['board games', 'dinner'],
30
28
  * };
31
29
  *
32
- * export function Example() {
30
+ * function Example() {
33
31
  * const [timeOfDay, setTimeOfDay] = useState<TimeOfDay>('morning');
34
32
  *
35
33
  * const activityOptions = activityOptionsByTimeOfDay[timeOfDay];
@@ -56,41 +54,12 @@ import { useForceUpdate } from './useForceUpdate.js';
56
54
  * @param deps Dependencies that reset the state to `initialState`
57
55
  */
58
56
  export function useStateWithDeps(initialState, deps) {
59
- // It would be possible to use useState instead of useRef to store the state,
60
- // however this would trigger re-renders whenever the state is reset due to a
61
- // change in dependencies. In order to avoid these re-renders, the state is
62
- // stored in a ref, and updates are triggered with forceUpdate when necessary.
63
- const state = useRef(undefined);
64
- const prevDeps = useRef(deps);
65
- const isMounted = useRef(false);
66
- // If first render, or if dependencies have changed since last time
67
- if (!isMounted.current || !depsAreEqual(prevDeps.current, deps)) {
68
- // Update state and deps
69
- let nextState;
70
- if (isFunction(initialState)) {
71
- nextState = initialState(state.current);
72
- }
73
- else {
74
- nextState = initialState;
75
- }
76
- state.current = nextState;
77
- prevDeps.current = deps;
78
- isMounted.current = true;
57
+ const [state, setState] = useState(initialState);
58
+ const [prevDeps, setPrevDeps] = useState(deps);
59
+ if (!depsAreEqual(deps, prevDeps)) {
60
+ setPrevDeps(deps);
61
+ setState(initialState);
79
62
  }
80
- const [forceUpdate] = useForceUpdate();
81
- const updateState = useCallback(function updateState(newState) {
82
- let nextState;
83
- if (isFunction(newState)) {
84
- nextState = newState(state.current);
85
- }
86
- else {
87
- nextState = newState;
88
- }
89
- if (!Object.is(state.current, nextState)) {
90
- state.current = nextState;
91
- forceUpdate();
92
- }
93
- }, []); // eslint-disable-line react-hooks/exhaustive-deps
94
- return [state.current, updateState];
63
+ return [state, setState];
95
64
  }
96
65
  //# sourceMappingURL=useStateWithDeps.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useStateWithDeps.js","sourceRoot":"","sources":["../../src/hooks/useStateWithDeps.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,WAAW,EACX,MAAM,GAIP,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,MAAM,UAAU,gBAAgB,CAC9B,YAA4C,EAC5C,IAAoB;IAEpB,6EAA6E;IAC7E,6EAA6E;IAC7E,2EAA2E;IAC3E,8EAA8E;IAC9E,MAAM,KAAK,GAAG,MAAM,CAAC,SAAc,CAAC,CAAC;IAErC,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAC9B,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAEhC,mEAAmE;IACnE,IAAI,CAAC,SAAS,CAAC,OAAO,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,EAAE,CAAC;QAChE,wBAAwB;QACxB,IAAI,SAAY,CAAC;QACjB,IAAI,UAAU,CAAC,YAAY,CAAC,EAAE,CAAC;YAC7B,SAAS,GAAG,YAAY,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC1C,CAAC;aAAM,CAAC;YACN,SAAS,GAAG,YAAY,CAAC;QAC3B,CAAC;QACD,KAAK,CAAC,OAAO,GAAG,SAAS,CAAC;QAC1B,QAAQ,CAAC,OAAO,GAAG,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CAAC,IAAI,CAAC,CAAC;QAClB,QAAQ,CAAC,YAAY,CAAC,CAAC;IACzB,CAAC;IAED,OAAO,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;AAC3B,CAAC"}
@@ -6,7 +6,8 @@ import type { ArgumentFallback } from '../utils.js';
6
6
  * as an argument to `useContext` or `use` (the hook produced with
7
7
  * {@linkcode createSafeContext} should be used instead)
8
8
  *
9
- * @see {@linkcode createSafeContext}
9
+ * @see
10
+ * {@linkcode createSafeContext}
10
11
  */
11
12
  export type RestrictedContext<T> = Context<T> extends Provider<T> ? {
12
13
  Provider: Provider<T>;
@@ -18,7 +19,9 @@ export type RestrictedContext<T> = Context<T> extends Provider<T> ? {
18
19
  /**
19
20
  * The return type of {@linkcode createSafeContext}
20
21
  *
21
- * @see {@linkcode createSafeContext}
22
+ * @see
23
+ * {@linkcode createSafeContext},
24
+ * {@linkcode RestrictedContext}
22
25
  */
23
26
  export type SafeContext<DisplayName extends string, T> = {
24
27
  [K in `${DisplayName}Context`]: RestrictedContext<T>;
@@ -80,6 +83,9 @@ export type SafeContext<DisplayName extends string, T> = {
80
83
  * - ``` `${displayName}Context` ``` (e.g. `DirectionContext`): the context
81
84
  * - ``` `use${displayName}` ``` (e.g. `useDirection`): a hook that returns the
82
85
  * current context value if one was provided, or throws an error otherwise
86
+ *
87
+ * @see
88
+ * {@linkcode SafeContext}
83
89
  */
84
90
  export declare function createSafeContext<T = never>(): <DisplayName extends string>(displayName: [T] extends [never] ? never : ArgumentFallback<DisplayName, never, string>) => SafeContext<DisplayName, T>;
85
91
  //# sourceMappingURL=createSafeContext.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createSafeContext.d.ts","sourceRoot":"","sources":["../../src/misc/createSafeContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,QAAQ,EAA6B,MAAM,OAAO,CAAC;AAC/E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;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C,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;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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"}
@@ -55,6 +55,9 @@ const moValueSymbol = Symbol('noValue');
55
55
  * - ``` `${displayName}Context` ``` (e.g. `DirectionContext`): the context
56
56
  * - ``` `use${displayName}` ``` (e.g. `useDirection`): a hook that returns the
57
57
  * current context value if one was provided, or throws an error otherwise
58
+ *
59
+ * @see
60
+ * {@linkcode SafeContext}
58
61
  */
59
62
  export function createSafeContext() {
60
63
  return (displayName) => {
@@ -1 +1 @@
1
- {"version":3,"file":"createSafeContext.js","sourceRoot":"","sources":["../../src/misc/createSafeContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+B,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAG/E,MAAM,aAAa,GAAG,MAAM,CAAC,SAAS,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AG,WAAW,CAAC;QAElC,OAAO;YACL,CAAC,WAAW,CAAC,EAAE,OAA+B;YAC9C,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE;gBACf,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;gBAClC,IAAI,KAAK,KAAK,aAAa,EAAE,CAAC;oBAC5B,MAAM,IAAI,KAAK,CAAC,MAAM,WAAW,qBAAqB,CAAC,CAAC;gBAC1D,CAAC;gBACD,OAAO,KAAK,CAAC;YACf,CAAC;SAKF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
package/dist/utils.d.ts CHANGED
@@ -1,7 +1,4 @@
1
1
  import type { DependencyList } from 'react';
2
- export type Callable = (...args: never) => unknown;
3
2
  export type ArgumentFallback<T extends Base, Default extends Base, Base = unknown> = [T] extends [never] ? Default : [Base] extends [T] ? Default : T;
4
- export declare function noop(): void;
5
- export declare function isFunction(input: unknown): input is Callable;
6
3
  export declare function depsAreEqual(prevDeps: DependencyList, deps: DependencyList): boolean;
7
4
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAE5C,MAAM,MAAM,QAAQ,GAAG,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC;AAEnD,MAAM,MAAM,gBAAgB,CAC1B,CAAC,SAAS,IAAI,EACd,OAAO,SAAS,IAAI,EACpB,IAAI,GAAG,OAAO,IACZ,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,OAAO,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,OAAO,GAAG,CAAC,CAAC;AAErE,wBAAgB,IAAI,SAAK;AAEzB,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,QAAQ,CAE5D;AAED,wBAAgB,YAAY,CAC1B,QAAQ,EAAE,cAAc,EACxB,IAAI,EAAE,cAAc,GACnB,OAAO,CAKT"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAE5C,MAAM,MAAM,gBAAgB,CAC1B,CAAC,SAAS,IAAI,EACd,OAAO,SAAS,IAAI,EACpB,IAAI,GAAG,OAAO,IACZ,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,OAAO,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,OAAO,GAAG,CAAC,CAAC;AAErE,wBAAgB,YAAY,CAC1B,QAAQ,EAAE,cAAc,EACxB,IAAI,EAAE,cAAc,GACnB,OAAO,CAKT"}