2626 <img src="https://custom-icon-badges.demolab.com/npm/dm/@better-hooks/performance?logoColor=fff&logo=trending-up" />
2727 </a >
2828</p >
29+
2930## About
3031
31- Debounce and throttle your functions to gain performance boost!
32+ Debounce and throttle your functions, state and effects to gain performance boost!
3233
3334## Key Features
3435
@@ -38,6 +39,10 @@ Debounce and throttle your functions to gain performance boost!
3839
3940✨ ** Debounce and Throttle**
4041
42+ 🎊 ** Lifecycle debounce and throttle**
43+
44+ 📡 ** State debounce and throttle**
45+
4146## Installation
4247
4348``` bash
@@ -56,14 +61,15 @@ yarn add @better-hooks/performance
5661
5762#### useDebounce
5863
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.
6066
6167``` tsx
6268import React from " react" ;
6369import { useDebounce } from " @better-hooks/performance" ;
6470
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 } )
6773
6874 // Standard usage
6975 const onTyping = () => {
@@ -76,7 +82,7 @@ const MyComponent: React.FC = ({ delay }) => {
7682 const onTypingDynamic = (value : string , customDelay : number ) => {
7783 debounce (() => {
7884 // debounced logic
79- }, customDelay )
85+ }, { delay: customDelay } )
8086 }
8187
8288 return (
@@ -86,6 +92,57 @@ const MyComponent: React.FC = ({ delay }) => {
8692
8793```
8894
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+
89146#### useThrottle
90147
91148This hook allows debouncing of the given function.
@@ -117,3 +174,74 @@ const MyComponent: React.FC = ({ delay }) => {
117174}
118175
119176```
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