File tree Expand file tree Collapse file tree 5 files changed +25
-13
lines changed
Expand file tree Collapse file tree 5 files changed +25
-13
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,6 @@ const Demo = () => {
88 < p > Resize the box to see changes</ p >
99 < textarea
1010 ref = { elementSize . ref }
11- className = '200px 200px'
1211 style = { { resize : 'both' } }
1312 value = { `width: ${ elementSize . value . width } \nheight: ${ elementSize . value . height } ` }
1413 />
Original file line number Diff line number Diff line change @@ -68,10 +68,10 @@ export const useElementSize = ((...params: any[]) => {
6868 useEffect ( ( ) => {
6969 if ( ! target && ! internalRef ) return ;
7070 const element = ( target ? getElement ( target ) : internalRef ) as Element ;
71- if ( element ) return ;
71+
72+ if ( ! element ) return ;
7273 const observer = new ResizeObserver ( ( [ entry ] ) => {
7374 const { inlineSize : width , blockSize : height } = entry . borderBoxSize [ 0 ] ;
74-
7575 setSize ( { width, height } ) ;
7676 } ) ;
7777
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import { useThrottleValue } from './useThrottleValue';
44const Demo = ( ) => {
55 const counter = useCounter ( ) ;
66
7- const throttledCounterCount = useThrottleValue ( counter . value , 500 ) ;
7+ const throttledCounterCount = useThrottleValue ( counter . value , 2000 ) ;
88
99 return (
1010 < div >
Original file line number Diff line number Diff line change @@ -17,9 +17,9 @@ import { useThrottleCallback } from '../useThrottleCallback/useThrottleCallback'
1717 */
1818export const useThrottleValue = < Value > ( value : Value , delay : number ) => {
1919 const previousValueRef = useRef ( value ) ;
20- const [ throttledValue , setDebounceValue ] = useState ( value ) ;
20+ const [ throttledValue , setThrottleValue ] = useState ( value ) ;
2121
22- const throttledSetState = useThrottleCallback ( setDebounceValue , delay ) ;
22+ const throttledSetState = useThrottleCallback ( setThrottleValue , delay ) ;
2323
2424 useEffect ( ( ) => {
2525 if ( previousValueRef . current === value ) return ;
Original file line number Diff line number Diff line change @@ -3,14 +3,27 @@ export const throttle = <Params extends any[]>(
33 delay : number
44) : ( ( ...args : Params ) => void ) => {
55 let isCalled = false ;
6+ let lastArgs : Params | null = null ;
67
7- return function ( this : any , ...args ) {
8- if ( ! isCalled ) {
9- callback . apply ( this , args ) ;
10- isCalled = true ;
11- setTimeout ( ( ) => {
12- isCalled = false ;
13- } , delay ) ;
8+ const timer = ( ) => {
9+ if ( ! lastArgs ) {
10+ isCalled = false ;
11+ return ;
1412 }
13+
14+ callback . apply ( this , lastArgs ) ;
15+ lastArgs = null ;
16+ setTimeout ( timer , delay ) ;
17+ } ;
18+
19+ return function ( this : any , ...args : Params ) {
20+ if ( isCalled ) {
21+ lastArgs = args ;
22+ return ;
23+ }
24+
25+ callback . apply ( this , args ) ;
26+ isCalled = true ;
27+ setTimeout ( timer , delay ) ;
1528 } ;
1629} ;
You can’t perform that action at this time.
0 commit comments