react-simplikit 0.0.52 → 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/hooks/usePrevious/usePrevious.cjs +1 -0
- package/dist/hooks/usePrevious/usePrevious.mjs +1 -0
- package/dist/hooks/useThrottledCallback/useThrottledCallback.cjs +9 -1
- package/dist/hooks/useThrottledCallback/useThrottledCallback.mjs +9 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ We are repositioning react-simplikit to focus exclusively on **platform-independ
|
|
|
27
27
|
Hooks that don't depend on specific platform APIs will continue to be actively maintained:
|
|
28
28
|
|
|
29
29
|
- State management hooks like `useToggle`, `useBooleanState`, `useCounter`
|
|
30
|
-
- Lifecycle hooks like `usePrevious
|
|
30
|
+
- Lifecycle hooks like `usePrevious`
|
|
31
31
|
- Utility hooks like `useDebounce`, `useThrottle`
|
|
32
32
|
- **Backward compatibility (BC) is preserved** for these existing pure logic hooks
|
|
33
33
|
|
|
@@ -69,37 +69,46 @@ pnpm add react-simplikit
|
|
|
69
69
|
## Quick Start
|
|
70
70
|
|
|
71
71
|
```tsx
|
|
72
|
-
import { useState
|
|
72
|
+
import { useState } from 'react';
|
|
73
73
|
import { useDebounce } from 'react-simplikit';
|
|
74
74
|
|
|
75
75
|
function SearchInput() {
|
|
76
76
|
const [query, setQuery] = useState('');
|
|
77
|
-
const debouncedQuery = useDebounce(query, 300);
|
|
78
77
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
78
|
+
const debouncedSearch = useDebounce((value: string) => {
|
|
79
|
+
// Actual API call
|
|
80
|
+
searchAPI(value);
|
|
81
|
+
}, 300);
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<input
|
|
85
|
+
value={query}
|
|
86
|
+
onChange={e => {
|
|
87
|
+
setQuery(e.target.value);
|
|
88
|
+
debouncedSearch(e.target.value);
|
|
89
|
+
}}
|
|
90
|
+
placeholder="Enter search term"
|
|
91
|
+
/>
|
|
92
|
+
);
|
|
86
93
|
}
|
|
87
94
|
```
|
|
88
95
|
|
|
96
|
+
The debounced function exposes `.cancel()`, and pending calls are cancelled automatically when the component unmounts.
|
|
97
|
+
|
|
89
98
|
## What's Included
|
|
90
99
|
|
|
91
100
|
### Hooks
|
|
92
101
|
|
|
93
|
-
| Hook | Description
|
|
94
|
-
| ------------------------- |
|
|
95
|
-
| `useBooleanState` | Manage boolean state with handlers
|
|
96
|
-
| `useDebounce` | Debounce a
|
|
97
|
-
| `useDebouncedCallback` | Debounce
|
|
98
|
-
| `useInterval` | Set up intervals declaratively
|
|
99
|
-
| `useIntersectionObserver` | Observe element visibility
|
|
100
|
-
| `usePreservedCallback` | Stable callback reference
|
|
101
|
-
| `usePreservedReference` | Stable object reference
|
|
102
|
-
| ... | [See all hooks](https://react-simplikit.slash.page)
|
|
102
|
+
| Hook | Description |
|
|
103
|
+
| ------------------------- | ----------------------------------------------------- |
|
|
104
|
+
| `useBooleanState` | Manage boolean state with handlers |
|
|
105
|
+
| `useDebounce` | Debounce a callback function |
|
|
106
|
+
| `useDebouncedCallback` | Debounce an `onChange` callback via an options object |
|
|
107
|
+
| `useInterval` | Set up intervals declaratively |
|
|
108
|
+
| `useIntersectionObserver` | Observe element visibility |
|
|
109
|
+
| `usePreservedCallback` | Stable callback reference |
|
|
110
|
+
| `usePreservedReference` | Stable object reference |
|
|
111
|
+
| ... | [See all hooks](https://react-simplikit.slash.page) |
|
|
103
112
|
|
|
104
113
|
### Components
|
|
105
114
|
|
|
@@ -111,10 +120,11 @@ function SearchInput() {
|
|
|
111
120
|
|
|
112
121
|
### Utilities
|
|
113
122
|
|
|
114
|
-
| Utility
|
|
115
|
-
|
|
|
116
|
-
| `
|
|
117
|
-
| `
|
|
123
|
+
| Utility | Description |
|
|
124
|
+
| -------------- | --------------------------------------------------- |
|
|
125
|
+
| `buildContext` | Define React Context with less boilerplate |
|
|
126
|
+
| `mergeProps` | Merge props, composing `className`, `style`, events |
|
|
127
|
+
| `mergeRefs` | Combine multiple refs into a single ref |
|
|
118
128
|
|
|
119
129
|
## Documentation
|
|
120
130
|
|
|
@@ -21,6 +21,7 @@ const strictEquals = (prev, next) => prev === next;
|
|
|
21
21
|
* const previousCount = usePrevious(count);
|
|
22
22
|
*/
|
|
23
23
|
function usePrevious(state, compare = strictEquals) {
|
|
24
|
+
"use no memo";
|
|
24
25
|
const prevRef = (0, react.useRef)(state);
|
|
25
26
|
const currentRef = (0, react.useRef)(state);
|
|
26
27
|
const isFirstRender = (0, react.useRef)(true);
|
|
@@ -21,6 +21,7 @@ const strictEquals = (prev, next) => prev === next;
|
|
|
21
21
|
* const previousCount = usePrevious(count);
|
|
22
22
|
*/
|
|
23
23
|
function usePrevious(state, compare = strictEquals) {
|
|
24
|
+
"use no memo";
|
|
24
25
|
const prevRef = useRef(state);
|
|
25
26
|
const currentRef = useRef(state);
|
|
26
27
|
const isFirstRender = useRef(true);
|
|
@@ -5,6 +5,14 @@ const require_throttle = require("../useThrottle/throttle.cjs");
|
|
|
5
5
|
let react = require("react");
|
|
6
6
|
//#region src/hooks/useThrottledCallback/useThrottledCallback.ts
|
|
7
7
|
/**
|
|
8
|
+
* Marks that no value has been forwarded to `onChange` yet.
|
|
9
|
+
*
|
|
10
|
+
* The hook skips redundant invocations by comparing against the last forwarded value, but it
|
|
11
|
+
* receives no initial value from the caller. Seeding that comparison with `false` would treat the
|
|
12
|
+
* first `false` as redundant and swallow it, so an unreachable sentinel is used instead.
|
|
13
|
+
*/
|
|
14
|
+
const NOT_INVOKED = Symbol("NOT_INVOKED");
|
|
15
|
+
/**
|
|
8
16
|
* @description
|
|
9
17
|
* `useThrottledCallback` is a React hook that returns a throttled version of the provided callback function.
|
|
10
18
|
* The throttled callback will only be invoked at most once per specified interval.
|
|
@@ -28,7 +36,7 @@ let react = require("react");
|
|
|
28
36
|
function useThrottledCallback({ onChange, timeThreshold, edges = ["leading", "trailing"] }) {
|
|
29
37
|
const handleChange = require_usePreservedCallback.usePreservedCallback(onChange);
|
|
30
38
|
const ref = (0, react.useRef)({
|
|
31
|
-
value:
|
|
39
|
+
value: NOT_INVOKED,
|
|
32
40
|
clearPreviousThrottle: () => {}
|
|
33
41
|
});
|
|
34
42
|
(0, react.useEffect)(function cleanupThrottleOnUnmount() {
|
|
@@ -5,6 +5,14 @@ import { throttle } from "../useThrottle/throttle.mjs";
|
|
|
5
5
|
import { useCallback, useEffect, useRef } from "react";
|
|
6
6
|
//#region src/hooks/useThrottledCallback/useThrottledCallback.ts
|
|
7
7
|
/**
|
|
8
|
+
* Marks that no value has been forwarded to `onChange` yet.
|
|
9
|
+
*
|
|
10
|
+
* The hook skips redundant invocations by comparing against the last forwarded value, but it
|
|
11
|
+
* receives no initial value from the caller. Seeding that comparison with `false` would treat the
|
|
12
|
+
* first `false` as redundant and swallow it, so an unreachable sentinel is used instead.
|
|
13
|
+
*/
|
|
14
|
+
const NOT_INVOKED = Symbol("NOT_INVOKED");
|
|
15
|
+
/**
|
|
8
16
|
* @description
|
|
9
17
|
* `useThrottledCallback` is a React hook that returns a throttled version of the provided callback function.
|
|
10
18
|
* The throttled callback will only be invoked at most once per specified interval.
|
|
@@ -28,7 +36,7 @@ import { useCallback, useEffect, useRef } from "react";
|
|
|
28
36
|
function useThrottledCallback({ onChange, timeThreshold, edges = ["leading", "trailing"] }) {
|
|
29
37
|
const handleChange = usePreservedCallback(onChange);
|
|
30
38
|
const ref = useRef({
|
|
31
|
-
value:
|
|
39
|
+
value: NOT_INVOKED,
|
|
32
40
|
clearPreviousThrottle: () => {}
|
|
33
41
|
});
|
|
34
42
|
useEffect(function cleanupThrottleOnUnmount() {
|