react-simplikit 0.0.51 → 0.0.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -24
- package/dist/components/ImpressionArea/ImpressionArea.cjs +57 -0
- package/{esm/components/ImpressionArea/index.d.ts → dist/components/ImpressionArea/ImpressionArea.d.cts} +9 -8
- package/dist/components/ImpressionArea/ImpressionArea.d.mts +48 -0
- package/dist/components/ImpressionArea/ImpressionArea.mjs +57 -0
- package/dist/components/ImpressionArea/index.d.cts +2 -46
- package/dist/components/ImpressionArea/index.d.mts +3 -0
- package/dist/components/Separated/Separated.cjs +38 -0
- package/{esm/components/Separated/index.d.ts → dist/components/Separated/Separated.d.cts} +7 -7
- package/dist/components/Separated/Separated.d.mts +38 -0
- package/dist/components/Separated/Separated.mjs +38 -0
- package/dist/components/Separated/index.d.cts +2 -37
- package/dist/components/Separated/index.d.mts +3 -0
- package/dist/components/SwitchCase/SwitchCase.cjs +40 -0
- package/{esm/components/SwitchCase/index.d.ts → dist/components/SwitchCase/SwitchCase.d.cts} +7 -8
- package/dist/components/SwitchCase/SwitchCase.d.mts +45 -0
- package/dist/components/SwitchCase/SwitchCase.mjs +40 -0
- package/dist/components/SwitchCase/index.d.cts +2 -45
- package/dist/components/SwitchCase/index.d.mts +3 -0
- package/dist/hooks/useAsyncEffect/index.d.cts +2 -24
- package/dist/hooks/useAsyncEffect/index.d.mts +3 -0
- package/dist/hooks/useAsyncEffect/useAsyncEffect.cjs +39 -0
- package/{esm/hooks/useAsyncEffect/index.d.ts → dist/hooks/useAsyncEffect/useAsyncEffect.d.cts} +4 -3
- package/dist/hooks/useAsyncEffect/useAsyncEffect.d.mts +26 -0
- package/dist/hooks/useAsyncEffect/useAsyncEffect.mjs +39 -0
- package/dist/hooks/useBooleanState/useBooleanState.cjs +36 -0
- package/dist/hooks/useBooleanState/{index.d.cts → useBooleanState.d.cts} +4 -2
- package/{esm/hooks/useBooleanState/index.d.ts → dist/hooks/useBooleanState/useBooleanState.d.mts} +4 -2
- package/dist/hooks/useBooleanState/useBooleanState.mjs +36 -0
- package/dist/hooks/useCallbackOncePerRender/index.d.cts +2 -39
- package/dist/hooks/useCallbackOncePerRender/index.d.mts +3 -0
- package/dist/hooks/useCallbackOncePerRender/useCallbackOncePerRender.cjs +52 -0
- package/{esm/hooks/useCallbackOncePerRender/index.d.ts → dist/hooks/useCallbackOncePerRender/useCallbackOncePerRender.d.cts} +4 -3
- package/dist/hooks/useCallbackOncePerRender/useCallbackOncePerRender.d.mts +41 -0
- package/dist/hooks/useCallbackOncePerRender/useCallbackOncePerRender.mjs +52 -0
- package/dist/hooks/useConditionalEffect/index.d.cts +2 -42
- package/dist/hooks/useConditionalEffect/index.d.mts +3 -0
- package/dist/hooks/useConditionalEffect/useConditionalEffect.cjs +57 -0
- package/{esm/hooks/useConditionalEffect/index.d.ts → dist/hooks/useConditionalEffect/useConditionalEffect.d.cts} +4 -3
- package/dist/hooks/useConditionalEffect/useConditionalEffect.d.mts +44 -0
- package/dist/hooks/useConditionalEffect/useConditionalEffect.mjs +57 -0
- package/dist/hooks/useControlledState/index.d.cts +2 -50
- package/dist/hooks/useControlledState/index.d.mts +3 -0
- package/dist/hooks/useControlledState/useControlledState.cjs +60 -0
- package/{esm/hooks/useControlledState/index.d.ts → dist/hooks/useControlledState/useControlledState.d.cts} +11 -10
- package/dist/hooks/useControlledState/useControlledState.d.mts +52 -0
- package/dist/hooks/useControlledState/useControlledState.mjs +60 -0
- package/dist/hooks/useCounter/useCounter.cjs +75 -0
- package/{esm/hooks/useCounter/index.d.ts → dist/hooks/useCounter/useCounter.d.cts} +12 -10
- package/dist/hooks/useCounter/{index.d.cts → useCounter.d.mts} +12 -10
- package/dist/hooks/useCounter/useCounter.mjs +75 -0
- package/dist/hooks/useDebounce/debounce.cjs +49 -0
- package/dist/hooks/useDebounce/debounce.d.cts +8 -0
- package/dist/hooks/useDebounce/debounce.d.mts +8 -0
- package/dist/hooks/useDebounce/debounce.mjs +49 -0
- package/dist/hooks/useDebounce/useDebounce.cjs +66 -0
- package/{esm/hooks/useDebounce/index.d.ts → dist/hooks/useDebounce/useDebounce.d.cts} +6 -8
- package/dist/hooks/useDebounce/{index.d.cts → useDebounce.d.mts} +6 -8
- package/dist/hooks/useDebounce/useDebounce.mjs +66 -0
- package/dist/hooks/useDebouncedCallback/useDebouncedCallback.cjs +62 -0
- package/{esm/hooks/useDebouncedCallback/index.d.ts → dist/hooks/useDebouncedCallback/useDebouncedCallback.d.cts} +9 -7
- package/dist/hooks/useDebouncedCallback/{index.d.cts → useDebouncedCallback.d.mts} +9 -7
- package/dist/hooks/useDebouncedCallback/useDebouncedCallback.mjs +62 -0
- package/dist/hooks/useDoubleClick/index.d.cts +2 -45
- package/dist/hooks/useDoubleClick/index.d.mts +3 -0
- package/dist/hooks/useDoubleClick/useDoubleClick.cjs +64 -0
- package/{esm/hooks/useDoubleClick/index.d.ts → dist/hooks/useDoubleClick/useDoubleClick.d.cts} +8 -7
- package/dist/hooks/useDoubleClick/useDoubleClick.d.mts +47 -0
- package/dist/hooks/useDoubleClick/useDoubleClick.mjs +64 -0
- package/dist/hooks/useGeolocation/useGeolocation.cjs +210 -0
- package/{esm/hooks/useGeolocation/index.d.ts → dist/hooks/useGeolocation/useGeolocation.d.cts} +26 -24
- package/dist/hooks/useGeolocation/{index.d.cts → useGeolocation.d.mts} +26 -24
- package/dist/hooks/useGeolocation/useGeolocation.mjs +209 -0
- package/dist/hooks/useImpressionRef/useImpressionRef.cjs +67 -0
- package/{esm/hooks/useImpressionRef/index.d.ts → dist/hooks/useImpressionRef/useImpressionRef.d.cts} +10 -8
- package/dist/hooks/useImpressionRef/{index.d.cts → useImpressionRef.d.mts} +10 -8
- package/dist/hooks/useImpressionRef/useImpressionRef.mjs +67 -0
- package/dist/hooks/useInputState/index.d.cts +2 -22
- package/dist/hooks/useInputState/index.d.mts +3 -0
- package/dist/hooks/useInputState/useInputState.cjs +32 -0
- package/{esm/hooks/useInputState/index.d.ts → dist/hooks/useInputState/useInputState.d.cts} +4 -3
- package/dist/hooks/useInputState/useInputState.d.mts +24 -0
- package/dist/hooks/useInputState/useInputState.mjs +32 -0
- package/dist/hooks/useIntersectionObserver/useIntersectionObserver.cjs +58 -0
- package/dist/hooks/useIntersectionObserver/{index.d.cts → useIntersectionObserver.d.cts} +4 -2
- package/{esm/hooks/useIntersectionObserver/index.d.ts → dist/hooks/useIntersectionObserver/useIntersectionObserver.d.mts} +4 -2
- package/dist/hooks/useIntersectionObserver/useIntersectionObserver.mjs +58 -0
- package/dist/hooks/useInterval/useInterval.cjs +65 -0
- package/dist/hooks/useInterval/{index.d.cts → useInterval.d.cts} +7 -5
- package/{esm/hooks/useInterval/index.d.ts → dist/hooks/useInterval/useInterval.d.mts} +7 -5
- package/dist/hooks/useInterval/useInterval.mjs +65 -0
- package/dist/hooks/useIsClient/useIsClient.cjs +49 -0
- package/dist/hooks/useIsClient/{index.d.cts → useIsClient.d.cts} +4 -2
- package/{esm/hooks/useIsClient/index.d.ts → dist/hooks/useIsClient/useIsClient.d.mts} +4 -2
- package/dist/hooks/useIsClient/useIsClient.mjs +49 -0
- package/dist/hooks/useIsomorphicLayoutEffect/index.d.cts +2 -24
- package/dist/hooks/useIsomorphicLayoutEffect/index.d.mts +3 -0
- package/dist/hooks/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.cjs +25 -0
- package/{esm/hooks/useIsomorphicLayoutEffect/index.d.ts → dist/hooks/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.d.cts} +4 -3
- package/dist/hooks/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.d.mts +26 -0
- package/dist/hooks/useIsomorphicLayoutEffect/useIsomorphicLayoutEffect.mjs +25 -0
- package/dist/hooks/useList/useList.cjs +94 -0
- package/{esm/hooks/useList/index.d.ts → dist/hooks/useList/useList.d.cts} +10 -8
- package/dist/hooks/useList/{index.d.cts → useList.d.mts} +10 -8
- package/dist/hooks/useList/useList.mjs +94 -0
- package/dist/hooks/useLoading/useLoading.cjs +61 -0
- package/dist/hooks/useLoading/{index.d.cts → useLoading.d.cts} +4 -2
- package/{esm/hooks/useLoading/index.d.ts → dist/hooks/useLoading/useLoading.d.mts} +4 -2
- package/dist/hooks/useLoading/useLoading.mjs +61 -0
- package/dist/hooks/useLongPress/index.d.cts +2 -73
- package/dist/hooks/useLongPress/index.d.mts +3 -0
- package/dist/hooks/useLongPress/useLongPress.cjs +133 -0
- package/{esm/hooks/useLongPress/index.d.ts → dist/hooks/useLongPress/useLongPress.d.cts} +18 -17
- package/dist/hooks/useLongPress/useLongPress.d.mts +75 -0
- package/dist/hooks/useLongPress/useLongPress.mjs +133 -0
- package/dist/hooks/useMap/useMap.cjs +64 -0
- package/dist/hooks/useMap/{index.d.cts → useMap.d.cts} +12 -10
- package/{esm/hooks/useMap/index.d.ts → dist/hooks/useMap/useMap.d.mts} +12 -10
- package/dist/hooks/useMap/useMap.mjs +64 -0
- package/dist/hooks/useOutsideClickEffect/useOutsideClickEffect.cjs +51 -0
- package/dist/hooks/useOutsideClickEffect/{index.d.cts → useOutsideClickEffect.d.cts} +4 -2
- package/{esm/hooks/useOutsideClickEffect/index.d.ts → dist/hooks/useOutsideClickEffect/useOutsideClickEffect.d.mts} +4 -2
- package/dist/hooks/useOutsideClickEffect/useOutsideClickEffect.mjs +51 -0
- package/dist/hooks/usePreservedCallback/usePreservedCallback.cjs +41 -0
- package/dist/hooks/usePreservedCallback/{index.d.cts → usePreservedCallback.d.cts} +4 -2
- package/{esm/hooks/usePreservedCallback/index.d.ts → dist/hooks/usePreservedCallback/usePreservedCallback.d.mts} +4 -2
- package/dist/hooks/usePreservedCallback/usePreservedCallback.mjs +41 -0
- package/dist/hooks/usePreservedReference/usePreservedReference.cjs +55 -0
- package/dist/hooks/usePreservedReference/{index.d.cts → usePreservedReference.d.cts} +4 -2
- package/{esm/hooks/usePreservedReference/index.d.ts → dist/hooks/usePreservedReference/usePreservedReference.d.mts} +4 -2
- package/dist/hooks/usePreservedReference/usePreservedReference.mjs +55 -0
- package/dist/hooks/usePrevious/usePrevious.cjs +39 -0
- package/{esm/hooks/usePrevious/index.d.ts → dist/hooks/usePrevious/usePrevious.d.cts} +4 -2
- package/dist/hooks/usePrevious/{index.d.cts → usePrevious.d.mts} +4 -2
- package/dist/hooks/usePrevious/usePrevious.mjs +39 -0
- package/dist/hooks/useRefEffect/index.d.cts +2 -30
- package/dist/hooks/useRefEffect/index.d.mts +3 -0
- package/dist/hooks/useRefEffect/useRefEffect.cjs +42 -0
- package/{esm/hooks/useRefEffect/index.d.ts → dist/hooks/useRefEffect/useRefEffect.d.cts} +4 -3
- package/dist/hooks/useRefEffect/useRefEffect.d.mts +32 -0
- package/dist/hooks/useRefEffect/useRefEffect.mjs +42 -0
- package/dist/hooks/useSet/useSet.cjs +79 -0
- package/dist/hooks/useSet/{index.d.cts → useSet.d.cts} +9 -7
- package/{esm/hooks/useSet/index.d.ts → dist/hooks/useSet/useSet.d.mts} +9 -7
- package/dist/hooks/useSet/useSet.mjs +79 -0
- package/dist/hooks/useStorageState/index.d.cts +2 -60
- package/dist/hooks/useStorageState/index.d.mts +3 -0
- package/dist/hooks/useStorageState/storage.cjs +85 -0
- package/dist/hooks/useStorageState/storage.d.cts +10 -0
- package/dist/hooks/useStorageState/storage.d.mts +10 -0
- package/dist/hooks/useStorageState/storage.mjs +80 -0
- package/dist/hooks/useStorageState/useStorageState.cjs +81 -0
- package/{esm/hooks/useStorageState/index.d.ts → dist/hooks/useStorageState/useStorageState.d.cts} +10 -15
- package/dist/hooks/useStorageState/useStorageState.d.mts +56 -0
- package/dist/hooks/useStorageState/useStorageState.mjs +81 -0
- package/dist/hooks/useThrottle/throttle.cjs +19 -0
- package/dist/hooks/useThrottle/throttle.d.cts +19 -0
- package/dist/hooks/useThrottle/throttle.d.mts +19 -0
- package/dist/hooks/useThrottle/throttle.mjs +19 -0
- package/dist/hooks/useThrottle/useThrottle.cjs +49 -0
- package/dist/hooks/useThrottle/{index.d.cts → useThrottle.d.cts} +6 -19
- package/{esm/hooks/useThrottle/index.d.ts → dist/hooks/useThrottle/useThrottle.d.mts} +6 -19
- package/dist/hooks/useThrottle/useThrottle.mjs +49 -0
- package/dist/hooks/useThrottledCallback/useThrottledCallback.cjs +65 -0
- package/{esm/hooks/useThrottledCallback/index.d.ts → dist/hooks/useThrottledCallback/useThrottledCallback.d.cts} +8 -6
- package/dist/hooks/useThrottledCallback/{index.d.cts → useThrottledCallback.d.mts} +8 -6
- package/dist/hooks/useThrottledCallback/useThrottledCallback.mjs +65 -0
- package/dist/hooks/useTimeout/useTimeout.cjs +40 -0
- package/dist/hooks/useTimeout/{index.d.cts → useTimeout.d.cts} +4 -2
- package/{esm/hooks/useTimeout/index.d.ts → dist/hooks/useTimeout/useTimeout.d.mts} +4 -2
- package/dist/hooks/useTimeout/useTimeout.mjs +40 -0
- package/dist/hooks/useToggle/useToggle.cjs +34 -0
- package/dist/hooks/useToggle/{index.d.cts → useToggle.d.cts} +4 -4
- package/{esm/hooks/useToggle/index.d.ts → dist/hooks/useToggle/useToggle.d.mts} +4 -4
- package/dist/hooks/useToggle/useToggle.mjs +34 -0
- package/dist/hooks/useVisibilityEvent/useVisibilityEvent.cjs +42 -0
- package/{esm/hooks/useVisibilityEvent/index.d.ts → dist/hooks/useVisibilityEvent/useVisibilityEvent.d.cts} +5 -3
- package/dist/hooks/useVisibilityEvent/{index.d.cts → useVisibilityEvent.d.mts} +5 -3
- package/dist/hooks/useVisibilityEvent/useVisibilityEvent.mjs +42 -0
- package/dist/index.cjs +77 -1397
- package/dist/index.d.cts +56 -40
- package/dist/index.d.mts +56 -0
- package/dist/index.mjs +40 -0
- package/dist/utils/buildContext/buildContext.cjs +50 -0
- package/{esm/utils/buildContext/index.d.ts → dist/utils/buildContext/buildContext.d.cts} +7 -7
- package/dist/utils/buildContext/buildContext.d.mts +38 -0
- package/dist/utils/buildContext/buildContext.mjs +50 -0
- package/dist/utils/buildContext/index.d.cts +2 -37
- package/dist/utils/buildContext/index.d.mts +3 -0
- package/dist/utils/mergeProps/index.d.cts +2 -25
- package/dist/utils/mergeProps/index.d.mts +3 -0
- package/dist/utils/mergeProps/mergeProps.cjs +53 -0
- package/{esm/utils/mergeProps/index.d.ts → dist/utils/mergeProps/mergeProps.d.cts} +7 -8
- package/dist/utils/mergeProps/mergeProps.d.mts +25 -0
- package/dist/utils/mergeProps/mergeProps.mjs +53 -0
- package/dist/utils/mergeRefs/index.d.cts +2 -38
- package/dist/utils/mergeRefs/index.d.mts +3 -0
- package/dist/utils/mergeRefs/mergeRefs.cjs +50 -0
- package/{esm/utils/mergeRefs/index.d.ts → dist/utils/mergeRefs/mergeRefs.d.cts} +4 -3
- package/dist/utils/mergeRefs/mergeRefs.d.mts +40 -0
- package/dist/utils/mergeRefs/mergeRefs.mjs +50 -0
- package/package.json +9 -11
- package/dist/components/ImpressionArea/index.cjs +0 -295
- package/dist/components/Separated/index.cjs +0 -41
- package/dist/components/SwitchCase/index.cjs +0 -36
- package/dist/hooks/useAsyncEffect/index.cjs +0 -49
- package/dist/hooks/useBooleanState/index.cjs +0 -46
- package/dist/hooks/useCallbackOncePerRender/index.cjs +0 -63
- package/dist/hooks/useConditionalEffect/index.cjs +0 -51
- package/dist/hooks/useControlledState/index.cjs +0 -57
- package/dist/hooks/useCounter/index.cjs +0 -70
- package/dist/hooks/useDebounce/index.cjs +0 -130
- package/dist/hooks/useDebouncedCallback/index.cjs +0 -148
- package/dist/hooks/useDoubleClick/index.cjs +0 -79
- package/dist/hooks/useGeolocation/index.cjs +0 -154
- package/dist/hooks/useImpressionRef/index.cjs +0 -258
- package/dist/hooks/useInputState/index.cjs +0 -46
- package/dist/hooks/useIntersectionObserver/index.cjs +0 -95
- package/dist/hooks/useInterval/index.cjs +0 -84
- package/dist/hooks/useIsClient/index.cjs +0 -40
- package/dist/hooks/useIsomorphicLayoutEffect/index.cjs +0 -35
- package/dist/hooks/useList/index.cjs +0 -104
- package/dist/hooks/useLoading/index.cjs +0 -65
- package/dist/hooks/useLongPress/index.cjs +0 -127
- package/dist/hooks/useMap/index.cjs +0 -78
- package/dist/hooks/useOutsideClickEffect/index.cjs +0 -74
- package/dist/hooks/usePreservedCallback/index.cjs +0 -45
- package/dist/hooks/usePreservedReference/index.cjs +0 -45
- package/dist/hooks/usePrevious/index.cjs +0 -48
- package/dist/hooks/useRefEffect/index.cjs +0 -72
- package/dist/hooks/useSet/index.cjs +0 -105
- package/dist/hooks/useStorageState/index.cjs +0 -196
- package/dist/hooks/useThrottle/index.cjs +0 -151
- package/dist/hooks/useThrottledCallback/index.cjs +0 -172
- package/dist/hooks/useTimeout/index.cjs +0 -57
- package/dist/hooks/useToggle/index.cjs +0 -37
- package/dist/hooks/useVisibilityEvent/index.cjs +0 -47
- package/dist/utils/buildContext/index.cjs +0 -56
- package/dist/utils/mergeProps/index.cjs +0 -71
- package/dist/utils/mergeRefs/index.cjs +0 -46
- package/esm/components/ImpressionArea/index.js +0 -269
- package/esm/components/Separated/index.js +0 -15
- package/esm/components/SwitchCase/index.js +0 -10
- package/esm/hooks/useAsyncEffect/index.js +0 -23
- package/esm/hooks/useBooleanState/index.js +0 -20
- package/esm/hooks/useCallbackOncePerRender/index.js +0 -37
- package/esm/hooks/useConditionalEffect/index.js +0 -25
- package/esm/hooks/useControlledState/index.js +0 -31
- package/esm/hooks/useCounter/index.js +0 -44
- package/esm/hooks/useDebounce/index.js +0 -104
- package/esm/hooks/useDebouncedCallback/index.js +0 -122
- package/esm/hooks/useDoubleClick/index.js +0 -53
- package/esm/hooks/useGeolocation/index.js +0 -128
- package/esm/hooks/useImpressionRef/index.js +0 -232
- package/esm/hooks/useInputState/index.js +0 -20
- package/esm/hooks/useIntersectionObserver/index.js +0 -69
- package/esm/hooks/useInterval/index.js +0 -58
- package/esm/hooks/useIsClient/index.js +0 -14
- package/esm/hooks/useIsomorphicLayoutEffect/index.js +0 -9
- package/esm/hooks/useList/index.js +0 -78
- package/esm/hooks/useLoading/index.js +0 -39
- package/esm/hooks/useLongPress/index.js +0 -101
- package/esm/hooks/useMap/index.js +0 -52
- package/esm/hooks/useOutsideClickEffect/index.js +0 -48
- package/esm/hooks/usePreservedCallback/index.js +0 -19
- package/esm/hooks/usePreservedReference/index.js +0 -19
- package/esm/hooks/usePrevious/index.js +0 -22
- package/esm/hooks/useRefEffect/index.js +0 -46
- package/esm/hooks/useSet/index.js +0 -79
- package/esm/hooks/useStorageState/index.js +0 -170
- package/esm/hooks/useThrottle/index.js +0 -125
- package/esm/hooks/useThrottledCallback/index.js +0 -146
- package/esm/hooks/useTimeout/index.js +0 -31
- package/esm/hooks/useToggle/index.js +0 -11
- package/esm/hooks/useVisibilityEvent/index.js +0 -21
- package/esm/index.d.ts +0 -40
- package/esm/index.js +0 -1335
- package/esm/utils/buildContext/index.js +0 -30
- package/esm/utils/mergeProps/index.js +0 -45
- package/esm/utils/mergeRefs/index.js +0 -20
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
//#region src/components/SwitchCase/SwitchCase.tsx
|
|
3
|
+
/**
|
|
4
|
+
* @description
|
|
5
|
+
* `SwitchCase` is a component that allows you to declaratively render components based on a given value,
|
|
6
|
+
* similar to a `switch-case` statement. It is useful when you need to conditionally render different
|
|
7
|
+
* components depending on a specific state.
|
|
8
|
+
*
|
|
9
|
+
* @param {Case} value - The value to compare against.
|
|
10
|
+
* The component associated with the matching key in `caseBy` will be rendered.
|
|
11
|
+
* @param {Partial<{ [P in StringifiedValue<Case>]: () => ReactElement | null }>} caseBy - An object that maps values to
|
|
12
|
+
* components to render. The keys represent possible values, and the values are functions returning
|
|
13
|
+
* the corresponding components.
|
|
14
|
+
* @param {() => ReactElement | null} [defaultComponent] - The component to render if `value` does not match
|
|
15
|
+
* any key in `caseBy`.
|
|
16
|
+
*
|
|
17
|
+
* @returns {ReactElement | null} A React component that conditionally renders based on cases.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* function App() {
|
|
21
|
+
* return (
|
|
22
|
+
* <SwitchCase
|
|
23
|
+
* value={status}
|
|
24
|
+
* // Renders TypeA, TypeB, or TypeC based on the status value.
|
|
25
|
+
* caseBy={{
|
|
26
|
+
* a: () => <TypeA />,
|
|
27
|
+
* b: () => <TypeB />,
|
|
28
|
+
* c: () => <TypeC />,
|
|
29
|
+
* }}
|
|
30
|
+
* // Renders Default when the status value does not match any case.
|
|
31
|
+
* defaultComponent={() => <Default />}
|
|
32
|
+
* />
|
|
33
|
+
* );
|
|
34
|
+
* }
|
|
35
|
+
*/
|
|
36
|
+
function SwitchCase({ value, caseBy, defaultComponent = () => null }) {
|
|
37
|
+
return (caseBy[String(value)] ?? defaultComponent)();
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
export { SwitchCase };
|
|
@@ -1,46 +1,3 @@
|
|
|
1
|
-
import { ReactElement } from 'react';
|
|
2
1
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
value: Case;
|
|
6
|
-
caseBy: Partial<{
|
|
7
|
-
[P in StringifiedValue<Case>]: () => ReactElement | null;
|
|
8
|
-
}>;
|
|
9
|
-
defaultComponent?: () => ReactElement | null;
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* @description
|
|
13
|
-
* `SwitchCase` is a component that allows you to declaratively render components based on a given value,
|
|
14
|
-
* similar to a `switch-case` statement. It is useful when you need to conditionally render different
|
|
15
|
-
* components depending on a specific state.
|
|
16
|
-
*
|
|
17
|
-
* @param {Case} value - The value to compare against.
|
|
18
|
-
* The component associated with the matching key in `caseBy` will be rendered.
|
|
19
|
-
* @param {Partial<{ [P in StringifiedValue<Case>]: () => ReactElement | null }>} caseBy - An object that maps values to
|
|
20
|
-
* components to render. The keys represent possible values, and the values are functions returning
|
|
21
|
-
* the corresponding components.
|
|
22
|
-
* @param {() => ReactElement | null} [defaultComponent] - The component to render if `value` does not match
|
|
23
|
-
* any key in `caseBy`.
|
|
24
|
-
*
|
|
25
|
-
* @returns {ReactElement | null} A React component that conditionally renders based on cases.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* function App() {
|
|
29
|
-
* return (
|
|
30
|
-
* <SwitchCase
|
|
31
|
-
* value={status}
|
|
32
|
-
* // Renders TypeA, TypeB, or TypeC based on the status value.
|
|
33
|
-
* caseBy={{
|
|
34
|
-
* a: () => <TypeA />,
|
|
35
|
-
* b: () => <TypeB />,
|
|
36
|
-
* c: () => <TypeC />,
|
|
37
|
-
* }}
|
|
38
|
-
* // Renders Default when the status value does not match any case.
|
|
39
|
-
* defaultComponent={() => <Default />}
|
|
40
|
-
* />
|
|
41
|
-
* );
|
|
42
|
-
* }
|
|
43
|
-
*/
|
|
44
|
-
declare function SwitchCase<Case>({ value, caseBy, defaultComponent }: Props<Case>): ReactElement | null;
|
|
45
|
-
|
|
46
|
-
export { SwitchCase };
|
|
2
|
+
import { SwitchCase } from "./SwitchCase.cjs";
|
|
3
|
+
export { SwitchCase };
|
|
@@ -1,25 +1,3 @@
|
|
|
1
|
-
import { DependencyList } from 'react';
|
|
2
1
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* `useAsyncEffect` is a React hook for handling asynchronous side effects in React components.
|
|
6
|
-
* It follows the same cleanup pattern as `useEffect` while ensuring async operations are handled safely.
|
|
7
|
-
*
|
|
8
|
-
* @param {() => Promise<void | (() => void)>} [effect] - An asynchronous function executed in the `useEffect` pattern.
|
|
9
|
-
* This function can optionally return a cleanup function.
|
|
10
|
-
* @param {DependencyList} [deps] - A dependency array.
|
|
11
|
-
* The effect will re-run whenever any value in this array changes. If omitted, it runs only once when the component mounts.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* useAsyncEffect(async () => {
|
|
15
|
-
* const data = await fetchData();
|
|
16
|
-
* setData(data);
|
|
17
|
-
*
|
|
18
|
-
* return () => {
|
|
19
|
-
* console.log('Cleanup on unmount or dependencies change');
|
|
20
|
-
* };
|
|
21
|
-
* }, [dependencies]);
|
|
22
|
-
*/
|
|
23
|
-
declare function useAsyncEffect(effect: () => Promise<void | (() => void)>, deps?: DependencyList): void;
|
|
24
|
-
|
|
25
|
-
export { useAsyncEffect };
|
|
2
|
+
import { useAsyncEffect } from "./useAsyncEffect.cjs";
|
|
3
|
+
export { useAsyncEffect };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/hooks/useAsyncEffect/useAsyncEffect.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useAsyncEffect` is a React hook for handling asynchronous side effects in React components.
|
|
7
|
+
* It follows the same cleanup pattern as `useEffect` while ensuring async operations are handled safely.
|
|
8
|
+
*
|
|
9
|
+
* @param {() => Promise<void | (() => void)>} [effect] - An asynchronous function executed in the `useEffect` pattern.
|
|
10
|
+
* This function can optionally return a cleanup function.
|
|
11
|
+
* @param {DependencyList} [deps] - A dependency array.
|
|
12
|
+
* The effect will re-run whenever any value in this array changes. If omitted, it runs only once when the component mounts.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* useAsyncEffect(async () => {
|
|
16
|
+
* const data = await fetchData();
|
|
17
|
+
* setData(data);
|
|
18
|
+
*
|
|
19
|
+
* return () => {
|
|
20
|
+
* console.log('Cleanup on unmount or dependencies change');
|
|
21
|
+
* };
|
|
22
|
+
* }, [dependencies]);
|
|
23
|
+
*/
|
|
24
|
+
function useAsyncEffect(effect, deps) {
|
|
25
|
+
(0, react.useEffect)(() => {
|
|
26
|
+
let cleanup;
|
|
27
|
+
let isCleaned = false;
|
|
28
|
+
effect().then((result) => {
|
|
29
|
+
cleanup = result;
|
|
30
|
+
if (isCleaned) cleanup?.();
|
|
31
|
+
});
|
|
32
|
+
return () => {
|
|
33
|
+
isCleaned = true;
|
|
34
|
+
cleanup?.();
|
|
35
|
+
};
|
|
36
|
+
}, deps);
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
exports.useAsyncEffect = useAsyncEffect;
|
package/{esm/hooks/useAsyncEffect/index.d.ts → dist/hooks/useAsyncEffect/useAsyncEffect.d.cts}
RENAMED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { DependencyList } from 'react';
|
|
2
1
|
|
|
2
|
+
import { DependencyList } from "react";
|
|
3
|
+
//#region src/hooks/useAsyncEffect/useAsyncEffect.d.ts
|
|
3
4
|
/**
|
|
4
5
|
* @description
|
|
5
6
|
* `useAsyncEffect` is a React hook for handling asynchronous side effects in React components.
|
|
@@ -21,5 +22,5 @@ import { DependencyList } from 'react';
|
|
|
21
22
|
* }, [dependencies]);
|
|
22
23
|
*/
|
|
23
24
|
declare function useAsyncEffect(effect: () => Promise<void | (() => void)>, deps?: DependencyList): void;
|
|
24
|
-
|
|
25
|
-
export { useAsyncEffect };
|
|
25
|
+
//#endregion
|
|
26
|
+
export { useAsyncEffect };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
|
|
2
|
+
import { DependencyList } from "react";
|
|
3
|
+
//#region src/hooks/useAsyncEffect/useAsyncEffect.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useAsyncEffect` is a React hook for handling asynchronous side effects in React components.
|
|
7
|
+
* It follows the same cleanup pattern as `useEffect` while ensuring async operations are handled safely.
|
|
8
|
+
*
|
|
9
|
+
* @param {() => Promise<void | (() => void)>} [effect] - An asynchronous function executed in the `useEffect` pattern.
|
|
10
|
+
* This function can optionally return a cleanup function.
|
|
11
|
+
* @param {DependencyList} [deps] - A dependency array.
|
|
12
|
+
* The effect will re-run whenever any value in this array changes. If omitted, it runs only once when the component mounts.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* useAsyncEffect(async () => {
|
|
16
|
+
* const data = await fetchData();
|
|
17
|
+
* setData(data);
|
|
18
|
+
*
|
|
19
|
+
* return () => {
|
|
20
|
+
* console.log('Cleanup on unmount or dependencies change');
|
|
21
|
+
* };
|
|
22
|
+
* }, [dependencies]);
|
|
23
|
+
*/
|
|
24
|
+
declare function useAsyncEffect(effect: () => Promise<void | (() => void)>, deps?: DependencyList): void;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { useAsyncEffect };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useEffect } from "react";
|
|
3
|
+
//#region src/hooks/useAsyncEffect/useAsyncEffect.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useAsyncEffect` is a React hook for handling asynchronous side effects in React components.
|
|
7
|
+
* It follows the same cleanup pattern as `useEffect` while ensuring async operations are handled safely.
|
|
8
|
+
*
|
|
9
|
+
* @param {() => Promise<void | (() => void)>} [effect] - An asynchronous function executed in the `useEffect` pattern.
|
|
10
|
+
* This function can optionally return a cleanup function.
|
|
11
|
+
* @param {DependencyList} [deps] - A dependency array.
|
|
12
|
+
* The effect will re-run whenever any value in this array changes. If omitted, it runs only once when the component mounts.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* useAsyncEffect(async () => {
|
|
16
|
+
* const data = await fetchData();
|
|
17
|
+
* setData(data);
|
|
18
|
+
*
|
|
19
|
+
* return () => {
|
|
20
|
+
* console.log('Cleanup on unmount or dependencies change');
|
|
21
|
+
* };
|
|
22
|
+
* }, [dependencies]);
|
|
23
|
+
*/
|
|
24
|
+
function useAsyncEffect(effect, deps) {
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
let cleanup;
|
|
27
|
+
let isCleaned = false;
|
|
28
|
+
effect().then((result) => {
|
|
29
|
+
cleanup = result;
|
|
30
|
+
if (isCleaned) cleanup?.();
|
|
31
|
+
});
|
|
32
|
+
return () => {
|
|
33
|
+
isCleaned = true;
|
|
34
|
+
cleanup?.();
|
|
35
|
+
};
|
|
36
|
+
}, deps);
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { useAsyncEffect };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region src/hooks/useBooleanState/useBooleanState.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useBooleanState` is a React hook that simplifies managing a boolean state.
|
|
7
|
+
* It provides functions to set the state to `true`, set it to `false`, and toggle its value.
|
|
8
|
+
*
|
|
9
|
+
* @param {boolean} [defaultValue=false] - The initial value of the state. Defaults to `false`.
|
|
10
|
+
*
|
|
11
|
+
* @returns {readonly [state: boolean, setTrue: () => void, setFalse: () => void, toggle: () => void]} A tuple containing:
|
|
12
|
+
* - state `boolean` - The current state value;
|
|
13
|
+
* - setTrue `() => void` - A function to set the state to `true`;
|
|
14
|
+
* - setFalse `() => void` - A function to set the state to `false`;
|
|
15
|
+
* - toggle `() => void` - A function to toggle the state;
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const [open, openBottomSheet, closeBottomSheet, toggleBottomSheet] = useBooleanState(false);
|
|
19
|
+
*/
|
|
20
|
+
function useBooleanState(defaultValue = false) {
|
|
21
|
+
const [bool, setBool] = (0, react.useState)(defaultValue);
|
|
22
|
+
return [
|
|
23
|
+
bool,
|
|
24
|
+
(0, react.useCallback)(() => {
|
|
25
|
+
setBool(true);
|
|
26
|
+
}, []),
|
|
27
|
+
(0, react.useCallback)(() => {
|
|
28
|
+
setBool(false);
|
|
29
|
+
}, []),
|
|
30
|
+
(0, react.useCallback)(() => {
|
|
31
|
+
setBool((prevBool) => !prevBool);
|
|
32
|
+
}, [])
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
exports.useBooleanState = useBooleanState;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useBooleanState/useBooleanState.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* @description
|
|
3
5
|
* `useBooleanState` is a React hook that simplifies managing a boolean state.
|
|
@@ -15,5 +17,5 @@
|
|
|
15
17
|
* const [open, openBottomSheet, closeBottomSheet, toggleBottomSheet] = useBooleanState(false);
|
|
16
18
|
*/
|
|
17
19
|
declare function useBooleanState(defaultValue?: boolean): readonly [boolean, () => void, () => void, () => void];
|
|
18
|
-
|
|
19
|
-
export { useBooleanState };
|
|
20
|
+
//#endregion
|
|
21
|
+
export { useBooleanState };
|
package/{esm/hooks/useBooleanState/index.d.ts → dist/hooks/useBooleanState/useBooleanState.d.mts}
RENAMED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/hooks/useBooleanState/useBooleanState.d.ts
|
|
1
3
|
/**
|
|
2
4
|
* @description
|
|
3
5
|
* `useBooleanState` is a React hook that simplifies managing a boolean state.
|
|
@@ -15,5 +17,5 @@
|
|
|
15
17
|
* const [open, openBottomSheet, closeBottomSheet, toggleBottomSheet] = useBooleanState(false);
|
|
16
18
|
*/
|
|
17
19
|
declare function useBooleanState(defaultValue?: boolean): readonly [boolean, () => void, () => void, () => void];
|
|
18
|
-
|
|
19
|
-
export { useBooleanState };
|
|
20
|
+
//#endregion
|
|
21
|
+
export { useBooleanState };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useState } from "react";
|
|
3
|
+
//#region src/hooks/useBooleanState/useBooleanState.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useBooleanState` is a React hook that simplifies managing a boolean state.
|
|
7
|
+
* It provides functions to set the state to `true`, set it to `false`, and toggle its value.
|
|
8
|
+
*
|
|
9
|
+
* @param {boolean} [defaultValue=false] - The initial value of the state. Defaults to `false`.
|
|
10
|
+
*
|
|
11
|
+
* @returns {readonly [state: boolean, setTrue: () => void, setFalse: () => void, toggle: () => void]} A tuple containing:
|
|
12
|
+
* - state `boolean` - The current state value;
|
|
13
|
+
* - setTrue `() => void` - A function to set the state to `true`;
|
|
14
|
+
* - setFalse `() => void` - A function to set the state to `false`;
|
|
15
|
+
* - toggle `() => void` - A function to toggle the state;
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const [open, openBottomSheet, closeBottomSheet, toggleBottomSheet] = useBooleanState(false);
|
|
19
|
+
*/
|
|
20
|
+
function useBooleanState(defaultValue = false) {
|
|
21
|
+
const [bool, setBool] = useState(defaultValue);
|
|
22
|
+
return [
|
|
23
|
+
bool,
|
|
24
|
+
useCallback(() => {
|
|
25
|
+
setBool(true);
|
|
26
|
+
}, []),
|
|
27
|
+
useCallback(() => {
|
|
28
|
+
setBool(false);
|
|
29
|
+
}, []),
|
|
30
|
+
useCallback(() => {
|
|
31
|
+
setBool((prevBool) => !prevBool);
|
|
32
|
+
}, [])
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { useBooleanState };
|
|
@@ -1,40 +1,3 @@
|
|
|
1
|
-
import { DependencyList } from 'react';
|
|
2
1
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* `useCallbackOncePerRender` is a React hook that ensures a callback function is executed only once, regardless of how many times it's called.
|
|
6
|
-
* This is useful for one-time operations that should not be repeated, even if the component re-renders.
|
|
7
|
-
*
|
|
8
|
-
* @param {() => void} callback - The callback function to be executed once.
|
|
9
|
-
* @param {DependencyList} deps - Dependencies array that will trigger a new one-time execution when changed.
|
|
10
|
-
*
|
|
11
|
-
* @returns {(...args: any[]) => void} A memoized function that will only execute once until dependencies change.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* import { useCallbackOncePerRender } from 'react-simplikit';
|
|
15
|
-
*
|
|
16
|
-
* function Component() {
|
|
17
|
-
* const handleOneTimeEvent = useCallbackOncePerRender(() => {
|
|
18
|
-
* console.log('This will only run once');
|
|
19
|
-
* }, []);
|
|
20
|
-
*
|
|
21
|
-
* return <button onClick={handleOneTimeEvent}>Click me</button>;
|
|
22
|
-
* }
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* // With dependencies
|
|
26
|
-
* function TrackingComponent({ userId }: { userId: string }) {
|
|
27
|
-
* const trackUserVisit = useCallbackOncePerRender(() => {
|
|
28
|
-
* analytics.trackVisit(userId);
|
|
29
|
-
* }, [userId]);
|
|
30
|
-
*
|
|
31
|
-
* useEffect(() => {
|
|
32
|
-
* trackUserVisit();
|
|
33
|
-
* }, [trackUserVisit]);
|
|
34
|
-
*
|
|
35
|
-
* return <div>User page</div>;
|
|
36
|
-
* }
|
|
37
|
-
*/
|
|
38
|
-
declare function useCallbackOncePerRender<F extends (...args: any[]) => void>(callback: F, deps: DependencyList): (...args: Parameters<F>) => void;
|
|
39
|
-
|
|
40
|
-
export { useCallbackOncePerRender };
|
|
2
|
+
import { useCallbackOncePerRender } from "./useCallbackOncePerRender.cjs";
|
|
3
|
+
export { useCallbackOncePerRender };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_usePreservedCallback = require("../usePreservedCallback/usePreservedCallback.cjs");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
//#region src/hooks/useCallbackOncePerRender/useCallbackOncePerRender.ts
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* `useCallbackOncePerRender` is a React hook that ensures a callback function is executed only once, regardless of how many times it's called.
|
|
8
|
+
* This is useful for one-time operations that should not be repeated, even if the component re-renders.
|
|
9
|
+
*
|
|
10
|
+
* @param {() => void} callback - The callback function to be executed once.
|
|
11
|
+
* @param {DependencyList} deps - Dependencies array that will trigger a new one-time execution when changed.
|
|
12
|
+
*
|
|
13
|
+
* @returns {(...args: any[]) => void} A memoized function that will only execute once until dependencies change.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* import { useCallbackOncePerRender } from 'react-simplikit';
|
|
17
|
+
*
|
|
18
|
+
* function Component() {
|
|
19
|
+
* const handleOneTimeEvent = useCallbackOncePerRender(() => {
|
|
20
|
+
* console.log('This will only run once');
|
|
21
|
+
* }, []);
|
|
22
|
+
*
|
|
23
|
+
* return <button onClick={handleOneTimeEvent}>Click me</button>;
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // With dependencies
|
|
28
|
+
* function TrackingComponent({ userId }: { userId: string }) {
|
|
29
|
+
* const trackUserVisit = useCallbackOncePerRender(() => {
|
|
30
|
+
* analytics.trackVisit(userId);
|
|
31
|
+
* }, [userId]);
|
|
32
|
+
*
|
|
33
|
+
* useEffect(() => {
|
|
34
|
+
* trackUserVisit();
|
|
35
|
+
* }, [trackUserVisit]);
|
|
36
|
+
*
|
|
37
|
+
* return <div>User page</div>;
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
function useCallbackOncePerRender(callback, deps) {
|
|
41
|
+
const hasFired = (0, react.useRef)(false);
|
|
42
|
+
(0, react.useEffect)(() => {
|
|
43
|
+
hasFired.current = false;
|
|
44
|
+
}, deps);
|
|
45
|
+
return require_usePreservedCallback.usePreservedCallback((...args) => {
|
|
46
|
+
if (hasFired.current) return;
|
|
47
|
+
callback(...args);
|
|
48
|
+
hasFired.current = true;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
exports.useCallbackOncePerRender = useCallbackOncePerRender;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { DependencyList } from 'react';
|
|
2
1
|
|
|
2
|
+
import { DependencyList } from "react";
|
|
3
|
+
//#region src/hooks/useCallbackOncePerRender/useCallbackOncePerRender.d.ts
|
|
3
4
|
/**
|
|
4
5
|
* @description
|
|
5
6
|
* `useCallbackOncePerRender` is a React hook that ensures a callback function is executed only once, regardless of how many times it's called.
|
|
@@ -36,5 +37,5 @@ import { DependencyList } from 'react';
|
|
|
36
37
|
* }
|
|
37
38
|
*/
|
|
38
39
|
declare function useCallbackOncePerRender<F extends (...args: any[]) => void>(callback: F, deps: DependencyList): (...args: Parameters<F>) => void;
|
|
39
|
-
|
|
40
|
-
export { useCallbackOncePerRender };
|
|
40
|
+
//#endregion
|
|
41
|
+
export { useCallbackOncePerRender };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
|
|
2
|
+
import { DependencyList } from "react";
|
|
3
|
+
//#region src/hooks/useCallbackOncePerRender/useCallbackOncePerRender.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* @description
|
|
6
|
+
* `useCallbackOncePerRender` is a React hook that ensures a callback function is executed only once, regardless of how many times it's called.
|
|
7
|
+
* This is useful for one-time operations that should not be repeated, even if the component re-renders.
|
|
8
|
+
*
|
|
9
|
+
* @param {() => void} callback - The callback function to be executed once.
|
|
10
|
+
* @param {DependencyList} deps - Dependencies array that will trigger a new one-time execution when changed.
|
|
11
|
+
*
|
|
12
|
+
* @returns {(...args: any[]) => void} A memoized function that will only execute once until dependencies change.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* import { useCallbackOncePerRender } from 'react-simplikit';
|
|
16
|
+
*
|
|
17
|
+
* function Component() {
|
|
18
|
+
* const handleOneTimeEvent = useCallbackOncePerRender(() => {
|
|
19
|
+
* console.log('This will only run once');
|
|
20
|
+
* }, []);
|
|
21
|
+
*
|
|
22
|
+
* return <button onClick={handleOneTimeEvent}>Click me</button>;
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* // With dependencies
|
|
27
|
+
* function TrackingComponent({ userId }: { userId: string }) {
|
|
28
|
+
* const trackUserVisit = useCallbackOncePerRender(() => {
|
|
29
|
+
* analytics.trackVisit(userId);
|
|
30
|
+
* }, [userId]);
|
|
31
|
+
*
|
|
32
|
+
* useEffect(() => {
|
|
33
|
+
* trackUserVisit();
|
|
34
|
+
* }, [trackUserVisit]);
|
|
35
|
+
*
|
|
36
|
+
* return <div>User page</div>;
|
|
37
|
+
* }
|
|
38
|
+
*/
|
|
39
|
+
declare function useCallbackOncePerRender<F extends (...args: any[]) => void>(callback: F, deps: DependencyList): (...args: Parameters<F>) => void;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { useCallbackOncePerRender };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { usePreservedCallback } from "../usePreservedCallback/usePreservedCallback.mjs";
|
|
3
|
+
import { useEffect, useRef } from "react";
|
|
4
|
+
//#region src/hooks/useCallbackOncePerRender/useCallbackOncePerRender.ts
|
|
5
|
+
/**
|
|
6
|
+
* @description
|
|
7
|
+
* `useCallbackOncePerRender` is a React hook that ensures a callback function is executed only once, regardless of how many times it's called.
|
|
8
|
+
* This is useful for one-time operations that should not be repeated, even if the component re-renders.
|
|
9
|
+
*
|
|
10
|
+
* @param {() => void} callback - The callback function to be executed once.
|
|
11
|
+
* @param {DependencyList} deps - Dependencies array that will trigger a new one-time execution when changed.
|
|
12
|
+
*
|
|
13
|
+
* @returns {(...args: any[]) => void} A memoized function that will only execute once until dependencies change.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* import { useCallbackOncePerRender } from 'react-simplikit';
|
|
17
|
+
*
|
|
18
|
+
* function Component() {
|
|
19
|
+
* const handleOneTimeEvent = useCallbackOncePerRender(() => {
|
|
20
|
+
* console.log('This will only run once');
|
|
21
|
+
* }, []);
|
|
22
|
+
*
|
|
23
|
+
* return <button onClick={handleOneTimeEvent}>Click me</button>;
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // With dependencies
|
|
28
|
+
* function TrackingComponent({ userId }: { userId: string }) {
|
|
29
|
+
* const trackUserVisit = useCallbackOncePerRender(() => {
|
|
30
|
+
* analytics.trackVisit(userId);
|
|
31
|
+
* }, [userId]);
|
|
32
|
+
*
|
|
33
|
+
* useEffect(() => {
|
|
34
|
+
* trackUserVisit();
|
|
35
|
+
* }, [trackUserVisit]);
|
|
36
|
+
*
|
|
37
|
+
* return <div>User page</div>;
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
function useCallbackOncePerRender(callback, deps) {
|
|
41
|
+
const hasFired = useRef(false);
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
hasFired.current = false;
|
|
44
|
+
}, deps);
|
|
45
|
+
return usePreservedCallback((...args) => {
|
|
46
|
+
if (hasFired.current) return;
|
|
47
|
+
callback(...args);
|
|
48
|
+
hasFired.current = true;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { useCallbackOncePerRender };
|
|
@@ -1,43 +1,3 @@
|
|
|
1
|
-
import { DependencyList, EffectCallback } from 'react';
|
|
2
1
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* `useConditionalEffect` is a React hook that conditionally executes effects based on a predicate function.
|
|
6
|
-
* This provides more control over when effects run beyond just dependency changes.
|
|
7
|
-
*
|
|
8
|
-
* @param {EffectCallback} effect - The effect callback to run.
|
|
9
|
-
* @param {DependencyList} deps - Dependencies array, similar to useEffect.
|
|
10
|
-
* @param {(prevDeps: T | undefined, currentDeps: T) => boolean} condition - Function that determines if the effect should run based on previous and current deps.
|
|
11
|
-
* - On the initial render, `prevDeps` will be `undefined`. Your `condition` function should handle this case.
|
|
12
|
-
* - If you want your effect to run on the initial render, return `true` when `prevDeps` is `undefined`.
|
|
13
|
-
* - If you don't want your effect to run on the initial render, return `false` when `prevDeps` is `undefined`.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* import { useConditionalEffect } from 'react-simplikit';
|
|
17
|
-
*
|
|
18
|
-
* function Component() {
|
|
19
|
-
* const [count, setCount] = useState(0);
|
|
20
|
-
*
|
|
21
|
-
* // Only run effect when count increases
|
|
22
|
-
* useConditionalEffect(
|
|
23
|
-
* () => {
|
|
24
|
-
* console.log(`Count increased to ${count}`);
|
|
25
|
-
* },
|
|
26
|
-
* [count],
|
|
27
|
-
* (prevDeps, currentDeps) => {
|
|
28
|
-
* // Only run when count is defined and has increased
|
|
29
|
-
* return prevDeps && currentDeps[0] > prevDeps[0];
|
|
30
|
-
* }
|
|
31
|
-
* );
|
|
32
|
-
*
|
|
33
|
-
* return (
|
|
34
|
-
* <button onClick={() => setCount(prev => prev + 1)}>
|
|
35
|
-
* Increment: {count}
|
|
36
|
-
* </button>
|
|
37
|
-
* );
|
|
38
|
-
* }
|
|
39
|
-
*
|
|
40
|
-
*/
|
|
41
|
-
declare function useConditionalEffect<T extends DependencyList>(effect: EffectCallback, deps: T, condition: (prevDeps: T | undefined, currentDeps: T) => boolean): void;
|
|
42
|
-
|
|
43
|
-
export { useConditionalEffect };
|
|
2
|
+
import { useConditionalEffect } from "./useConditionalEffect.cjs";
|
|
3
|
+
export { useConditionalEffect };
|