@aweebit/react-essentials 0.5.4 → 0.7.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.
Files changed (59) hide show
  1. package/README.md +836 -0
  2. package/dist/hooks/index.d.ts +5 -0
  3. package/dist/hooks/index.d.ts.map +1 -0
  4. package/dist/hooks/index.js +5 -0
  5. package/dist/hooks/index.js.map +1 -0
  6. package/dist/hooks/useEventListener.d.ts +65 -11
  7. package/dist/hooks/useEventListener.d.ts.map +1 -1
  8. package/dist/hooks/useEventListener.js +49 -18
  9. package/dist/hooks/useEventListener.js.map +1 -1
  10. package/dist/hooks/useForceUpdate.d.ts +52 -4
  11. package/dist/hooks/useForceUpdate.d.ts.map +1 -1
  12. package/dist/hooks/useForceUpdate.js +58 -9
  13. package/dist/hooks/useForceUpdate.js.map +1 -1
  14. package/dist/hooks/useReducerWithDeps.d.ts +17 -11
  15. package/dist/hooks/useReducerWithDeps.d.ts.map +1 -1
  16. package/dist/hooks/useReducerWithDeps.js +14 -12
  17. package/dist/hooks/useReducerWithDeps.js.map +1 -1
  18. package/dist/hooks/useStateWithDeps.d.ts +41 -7
  19. package/dist/hooks/useStateWithDeps.d.ts.map +1 -1
  20. package/dist/hooks/useStateWithDeps.js +47 -15
  21. package/dist/hooks/useStateWithDeps.js.map +1 -1
  22. package/dist/index.d.ts +2 -5
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +2 -5
  25. package/dist/index.js.map +1 -1
  26. package/dist/misc/createSafeContext.d.ts +85 -0
  27. package/dist/misc/createSafeContext.d.ts.map +1 -0
  28. package/dist/misc/createSafeContext.js +77 -0
  29. package/dist/misc/createSafeContext.js.map +1 -0
  30. package/dist/misc/index.d.ts +2 -0
  31. package/dist/misc/index.d.ts.map +1 -0
  32. package/dist/misc/index.js +2 -0
  33. package/dist/misc/index.js.map +1 -0
  34. package/dist/utils.d.ts +7 -0
  35. package/dist/utils.d.ts.map +1 -0
  36. package/dist/{utils/index.js → utils.js} +1 -2
  37. package/dist/utils.js.map +1 -0
  38. package/package.json +25 -15
  39. package/src/hooks/index.ts +4 -0
  40. package/src/hooks/useEventListener.ts +201 -0
  41. package/src/hooks/useForceUpdate.ts +91 -0
  42. package/{lib → src}/hooks/useReducerWithDeps.ts +27 -18
  43. package/src/hooks/useStateWithDeps.ts +111 -0
  44. package/src/index.ts +2 -0
  45. package/src/misc/createSafeContext.ts +116 -0
  46. package/src/misc/index.ts +1 -0
  47. package/{lib/utils/index.ts → src/utils.ts} +9 -2
  48. package/dist/hooks/useIsomorphicLayoutEffect.d.ts +0 -4
  49. package/dist/hooks/useIsomorphicLayoutEffect.d.ts.map +0 -1
  50. package/dist/hooks/useIsomorphicLayoutEffect.js +0 -5
  51. package/dist/hooks/useIsomorphicLayoutEffect.js.map +0 -1
  52. package/dist/utils/index.d.ts +0 -5
  53. package/dist/utils/index.d.ts.map +0 -1
  54. package/dist/utils/index.js.map +0 -1
  55. package/lib/hooks/useEventListener.ts +0 -101
  56. package/lib/hooks/useForceUpdate.ts +0 -40
  57. package/lib/hooks/useIsomorphicLayoutEffect.ts +0 -7
  58. package/lib/hooks/useStateWithDeps.ts +0 -79
  59. package/lib/index.ts +0 -5
@@ -1,79 +0,0 @@
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
-
8
- import {
9
- useCallback,
10
- useRef,
11
- type DependencyList,
12
- type Dispatch,
13
- type SetStateAction,
14
- } from 'react';
15
- import { depsAreEqual, isFunction } from '../utils/index.js';
16
- import useForceUpdate from './useForceUpdate.js';
17
-
18
- /**
19
- * `useState` hook with an additional dependency array that resets the state
20
- * to the `initialState` param when the dependencies passed in the `deps` array
21
- * change
22
- *
23
- * @param initialState The state that will be set when the component mounts or
24
- * the dependencies change
25
- *
26
- * It can also be a function which returns a state value. If the state is reset
27
- * due to a change of dependencies, this function will be passed the previous
28
- * state as its argument (will be `undefined` in the first call upon mount).
29
- *
30
- * @param deps Dependencies that reset the state to `initialState`
31
- */
32
- export default function useStateWithDeps<S>(
33
- initialState: S | ((previousState?: S) => S),
34
- deps: DependencyList,
35
- ): [S, Dispatch<SetStateAction<S>>] {
36
- // It would be possible to use useState instead of
37
- // useRef to store the state, however this would
38
- // trigger re-renders whenever the state is reset due
39
- // to a change in dependencies. In order to avoid these
40
- // re-renders, the state is stored in a ref and an
41
- // update is triggered via forceUpdate below when necessary
42
- const state = useRef(undefined as S);
43
-
44
- const prevDeps = useRef(deps);
45
- const isMounted = useRef(false);
46
-
47
- // If first render, or if dependencies have changed since last time
48
- if (!isMounted.current || !depsAreEqual(prevDeps.current, deps)) {
49
- // Update state and deps
50
- let nextState: S;
51
- if (isFunction(initialState)) {
52
- nextState = initialState(state.current);
53
- } else {
54
- nextState = initialState;
55
- }
56
- state.current = nextState;
57
- prevDeps.current = deps;
58
- isMounted.current = true;
59
- }
60
-
61
- const [forceUpdate] = useForceUpdate();
62
-
63
- const updateState = useCallback(function updateState(
64
- newState: S | ((previousState: S) => S),
65
- ): void {
66
- let nextState: S;
67
- if (isFunction(newState)) {
68
- nextState = newState(state.current);
69
- } else {
70
- nextState = newState;
71
- }
72
- if (!Object.is(state.current, nextState)) {
73
- state.current = nextState;
74
- forceUpdate();
75
- }
76
- }, []); // eslint-disable-line react-hooks/exhaustive-deps
77
-
78
- return [state.current, updateState];
79
- }
package/lib/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export { default as useEventListener } from './hooks/useEventListener.js';
2
- export { default as useForceUpdate } from './hooks/useForceUpdate.js';
3
- export { default as useIsomorphicLayoutEffect } from './hooks/useIsomorphicLayoutEffect.js';
4
- export { default as useReducerWithDeps } from './hooks/useReducerWithDeps.js';
5
- export { default as useStateWithDeps } from './hooks/useStateWithDeps.js';