26
26
<img src="https://custom-icon-badges.demolab.com/npm/dm/@better-hooks/performance?logoColor=fff&logo=trending-up" />
27
27
</a >
28
28
</p >
29
+
29
30
## About
30
31
31
- Debounce and throttle your functions to gain performance boost!
32
+ Debounce and throttle your functions, state and effects to gain performance boost!
32
33
33
34
## Key Features
34
35
@@ -38,6 +39,10 @@ Debounce and throttle your functions to gain performance boost!
38
39
39
40
✨ ** Debounce and Throttle**
40
41
42
+ 🎊 ** Lifecycle debounce and throttle**
43
+
44
+ 📡 ** State debounce and throttle**
45
+
41
46
## Installation
42
47
43
48
``` bash
@@ -56,14 +61,15 @@ yarn add @better-hooks/performance
56
61
57
62
#### useDebounce
58
63
59
- This hook allows debouncing of the given function.
64
+ This hook allows debouncing of the given function. Function will be called after some amount of time
65
+ from the last execution. We can adjust debounce time with additional props.
60
66
61
67
``` tsx
62
68
import React from " react" ;
63
69
import { useDebounce } from " @better-hooks/performance" ;
64
70
65
- const MyComponent: React .FC = ({ delay } ) => {
66
- const {debounce, reset, active} = useDebounce (delay )
71
+ const MyComponent: React .FC = () => {
72
+ const {debounce, reset, active} = useDebounce ({ delay: 600 } )
67
73
68
74
// Standard usage
69
75
const onTyping = () => {
@@ -76,7 +82,7 @@ const MyComponent: React.FC = ({ delay }) => {
76
82
const onTypingDynamic = (value : string , customDelay : number ) => {
77
83
debounce (() => {
78
84
// debounced logic
79
- }, customDelay )
85
+ }, { delay: customDelay } )
80
86
}
81
87
82
88
return (
@@ -86,6 +92,57 @@ const MyComponent: React.FC = ({ delay }) => {
86
92
87
93
```
88
94
95
+ ---
96
+
97
+ #### useDebounceState
98
+
99
+ This hook allows debouncing of state. Value will be saved after some amount of time from the last
100
+ execution of set function. We can adjust debounce time with additional props.
101
+
102
+ ``` tsx
103
+ import React from " react" ;
104
+ import { useWindowEvent } from " @better-hooks/window" ;
105
+ import { useDebounceState } from " @better-hooks/performance" ;
106
+
107
+ const MyComponent: React .FC = () => {
108
+ const [value, setValue] = useDebounceState (" 20px" )
109
+
110
+ useWindowEvent (" scroll" , (e ) => {
111
+ setValue (e .scrollY + " px" );
112
+ })
113
+
114
+ return (
115
+ // ...
116
+ )
117
+ }
118
+
119
+ ```
120
+
121
+ ---
122
+
123
+ #### useDebounceEffect
124
+
125
+ This hook allows debouncing of lifecycle effect.
126
+
127
+ ``` tsx
128
+ import React from " react" ;
129
+ import { useDebounceEffect } from " @better-hooks/performance" ;
130
+
131
+ const MyComponent: React .FC = (props ) => {
132
+
133
+ useDebounceEffect (() => {
134
+ // Do something
135
+ }, { delay: 200 }, [props ])
136
+
137
+ return (
138
+ // ...
139
+ )
140
+ }
141
+
142
+ ```
143
+
144
+ ---
145
+
89
146
#### useThrottle
90
147
91
148
This hook allows debouncing of the given function.
@@ -117,3 +174,74 @@ const MyComponent: React.FC = ({ delay }) => {
117
174
}
118
175
119
176
```
177
+
178
+ ---
179
+
180
+ #### useThrottleState
181
+
182
+ This hook allows throttling of state. We can adjust execution interval time and execution timeout
183
+ time with additional props.
184
+
185
+ ``` tsx
186
+ import React from " react" ;
187
+ import { useWindowEvent } from " @better-hooks/window" ;
188
+ import { useThrottleState } from " @better-hooks/performance" ;
189
+
190
+ const MyComponent: React .FC = () => {
191
+ const [value, setValue] = useThrottleState (" 20px" )
192
+
193
+ useWindowEvent (" scroll" , (e ) => {
194
+ setValue (e .scrollY + " px" );
195
+ })
196
+
197
+ return (
198
+ // ...
199
+ )
200
+ }
201
+
202
+ ```
203
+
204
+ ``` tsx
205
+ import React from " react" ;
206
+ import { useWindowEvent } from " @better-hooks/window" ;
207
+ import { useThrottleState } from " @better-hooks/performance" ;
208
+
209
+ const MyComponent: React .FC = () => {
210
+ const [value, setValue] = useThrottleState (" 20px" , {
211
+ executionInterval: 200 , // We will save values at least once per 200ms
212
+ executionTimeout: 400 // Last set state action will get triggered after 400ms, we can also disable it
213
+ })
214
+
215
+ useWindowEvent (" scroll" , (e ) => {
216
+ setValue (e .scrollY + " px" );
217
+ })
218
+
219
+ return (
220
+ // ...
221
+ )
222
+ }
223
+
224
+ ```
225
+
226
+ ---
227
+
228
+ #### useThrottleEffect
229
+
230
+ This hook allows debouncing of lifecycle effect.
231
+
232
+ ``` tsx
233
+ import React from " react" ;
234
+ import { useThrottleEffect } from " @better-hooks/performance" ;
235
+
236
+ const MyComponent: React .FC = (props ) => {
237
+
238
+ useThrottleEffect (() => {
239
+ // Do something
240
+ }, { executionInterval: 200 , executionTimeout: false }, [props ])
241
+
242
+ return (
243
+ // ...
244
+ )
245
+ }
246
+
247
+ ```
0 commit comments