@ttoss/react-hooks 1.25.0 → 1.25.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -11
- package/dist/esm/index.js +16 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +17 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/useDebounce.ts +17 -0
package/README.md
CHANGED
|
@@ -1,37 +1,64 @@
|
|
|
1
|
-
# @ttoss/hooks
|
|
1
|
+
# @ttoss/react-hooks
|
|
2
2
|
|
|
3
3
|
## 📚 About
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
**@ttoss/react-hooks** is an easy way to use Utility Hooks in your React application.
|
|
6
6
|
|
|
7
7
|
## 🚀 Getting Started
|
|
8
8
|
|
|
9
|
-
###
|
|
9
|
+
### Installing @ttoss/react-hooks
|
|
10
10
|
|
|
11
11
|
```shell
|
|
12
|
-
$ yarn add @ttoss/hooks
|
|
12
|
+
$ yarn add @ttoss/react-hooks
|
|
13
13
|
# or
|
|
14
|
-
$ npm install @ttoss/hooks
|
|
14
|
+
$ npm install @ttoss/react-hooks
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
## 📄 Examples
|
|
17
|
+
## 📄 Usage Examples
|
|
18
|
+
|
|
19
|
+
### useScript
|
|
20
|
+
|
|
21
|
+
The `useScript` hook is used to load external scripts into your React component.
|
|
18
22
|
|
|
19
23
|
```tsx
|
|
20
24
|
import React from 'react';
|
|
25
|
+
import { useScript } from '@ttoss/react-hooks';
|
|
21
26
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
export const Component = () => {
|
|
27
|
+
export const ComponentWithScript = () => {
|
|
25
28
|
const url = 'https://your-domain.com/bundle-api.js';
|
|
26
|
-
|
|
27
29
|
const { status } = useScript(url);
|
|
28
30
|
|
|
29
|
-
return <div>{status === 'ready' ? '
|
|
31
|
+
return <div>{status === 'ready' ? 'Script loaded' : 'Loading'}</div>;
|
|
32
|
+
};
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### useDebounce
|
|
36
|
+
|
|
37
|
+
The `useDebounce` hook is used to delay the update of a value for a specific amount of time.
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
import React, { useState } from 'react';
|
|
41
|
+
import { useDebounce } from '@ttoss/react-hooks';
|
|
42
|
+
|
|
43
|
+
export const DebouncedInputComponent = () => {
|
|
44
|
+
const [inputValue, setInputValue] = useState('');
|
|
45
|
+
const debouncedValue = useDebounce(inputValue, 500);
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<input
|
|
49
|
+
type="text"
|
|
50
|
+
value={inputValue}
|
|
51
|
+
onChange={(e) => setInputValue(e.target.value)}
|
|
52
|
+
placeholder="Type to search..."
|
|
53
|
+
/>
|
|
54
|
+
);
|
|
30
55
|
};
|
|
31
56
|
```
|
|
32
57
|
|
|
33
58
|
## 📘 Types
|
|
34
59
|
|
|
60
|
+
### useScript
|
|
61
|
+
|
|
35
62
|
```ts
|
|
36
63
|
type ScriptStatus = 'idle' | 'loading' | 'ready' | 'error';
|
|
37
64
|
|
|
@@ -39,3 +66,9 @@ const useScript: (src: string) => {
|
|
|
39
66
|
status: ScriptStatus;
|
|
40
67
|
};
|
|
41
68
|
```
|
|
69
|
+
|
|
70
|
+
### useDebounce
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
const useDebounce: <T>(value: T, delay?: number) => T;
|
|
74
|
+
```
|
package/dist/esm/index.js
CHANGED
|
@@ -46,4 +46,19 @@ var useScript = src => {
|
|
|
46
46
|
status
|
|
47
47
|
};
|
|
48
48
|
};
|
|
49
|
-
|
|
49
|
+
|
|
50
|
+
// src/useDebounce.ts
|
|
51
|
+
import * as React2 from "react";
|
|
52
|
+
var useDebounce = (value, delay) => {
|
|
53
|
+
const [debouncedValue, setDebouncedValue] = React2.useState(value);
|
|
54
|
+
React2.useEffect(() => {
|
|
55
|
+
const timer = setTimeout(() => {
|
|
56
|
+
return setDebouncedValue(value);
|
|
57
|
+
}, delay || 500);
|
|
58
|
+
return () => {
|
|
59
|
+
clearTimeout(timer);
|
|
60
|
+
};
|
|
61
|
+
}, [value, delay]);
|
|
62
|
+
return debouncedValue;
|
|
63
|
+
};
|
|
64
|
+
export { useDebounce, useScript };
|
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -38,6 +38,7 @@ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
|
38
38
|
// src/index.ts
|
|
39
39
|
var src_exports = {};
|
|
40
40
|
__export(src_exports, {
|
|
41
|
+
useDebounce: () => useDebounce,
|
|
41
42
|
useScript: () => useScript
|
|
42
43
|
});
|
|
43
44
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -88,7 +89,23 @@ var useScript = src => {
|
|
|
88
89
|
status
|
|
89
90
|
};
|
|
90
91
|
};
|
|
92
|
+
|
|
93
|
+
// src/useDebounce.ts
|
|
94
|
+
var React2 = __toESM(require("react"));
|
|
95
|
+
var useDebounce = (value, delay) => {
|
|
96
|
+
const [debouncedValue, setDebouncedValue] = React2.useState(value);
|
|
97
|
+
React2.useEffect(() => {
|
|
98
|
+
const timer = setTimeout(() => {
|
|
99
|
+
return setDebouncedValue(value);
|
|
100
|
+
}, delay || 500);
|
|
101
|
+
return () => {
|
|
102
|
+
clearTimeout(timer);
|
|
103
|
+
};
|
|
104
|
+
}, [value, delay]);
|
|
105
|
+
return debouncedValue;
|
|
106
|
+
};
|
|
91
107
|
// Annotate the CommonJS export names for ESM import in node:
|
|
92
108
|
0 && (module.exports = {
|
|
109
|
+
useDebounce,
|
|
93
110
|
useScript
|
|
94
111
|
});
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
export const useDebounce = <T>(value: T, delay?: number): T => {
|
|
4
|
+
const [debouncedValue, setDebouncedValue] = React.useState<T>(value);
|
|
5
|
+
|
|
6
|
+
React.useEffect(() => {
|
|
7
|
+
const timer = setTimeout(() => {
|
|
8
|
+
return setDebouncedValue(value);
|
|
9
|
+
}, delay || 500);
|
|
10
|
+
|
|
11
|
+
return () => {
|
|
12
|
+
clearTimeout(timer);
|
|
13
|
+
};
|
|
14
|
+
}, [value, delay]);
|
|
15
|
+
|
|
16
|
+
return debouncedValue;
|
|
17
|
+
};
|