Skip to content

Commit

Permalink
Improve the efficiency of the useDebouncedInput hook (#53263)
Browse files Browse the repository at this point in the history
  • Loading branch information
WunderBart authored Aug 2, 2023
1 parent 9a03557 commit 979ef33
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { useDebounce } from '@wordpress/compose';

export default function useDebouncedInput( defaultValue = '' ) {
const [ input, setInput ] = useState( defaultValue );
const [ debounced, setter ] = useState( defaultValue );
const setDebounced = useDebounce( setter, 250 );
const [ debouncedInput, setDebouncedState ] = useState( defaultValue );

const setDebouncedInput = useDebounce( setDebouncedState, 250 );

useEffect( () => {
if ( debounced !== input ) {
setDebounced( input );
}
}, [ debounced, input ] );
return [ input, setInput, debounced ];
setDebouncedInput( input );
}, [ input ] );

return [ input, setInput, debouncedInput ];
}
15 changes: 8 additions & 7 deletions packages/edit-site/src/utils/use-debounced-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { useDebounce } from '@wordpress/compose';

export default function useDebouncedInput( defaultValue = '' ) {
const [ input, setInput ] = useState( defaultValue );
const [ debounced, setter ] = useState( defaultValue );
const setDebounced = useDebounce( setter, 250 );
const [ debouncedInput, setDebouncedState ] = useState( defaultValue );

const setDebouncedInput = useDebounce( setDebouncedState, 250 );

useEffect( () => {
if ( debounced !== input ) {
setDebounced( input );
}
}, [ debounced, input ] );
return [ input, setInput, debounced ];
setDebouncedInput( input );
}, [ input ] );

return [ input, setInput, debouncedInput ];
}

0 comments on commit 979ef33

Please sign in to comment.