-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core/presentation): Add useDebouncedValue react hook
renamed hooks files to '.hook.ts' and exported all from an index.ts
- Loading branch information
1 parent
6186e40
commit b384038
Showing
4 changed files
with
33 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './useLatestPromise'; | ||
export * from './useData'; | ||
export * from './useData.hook'; | ||
export * from './useDebouncedValue.hook'; | ||
export * from './useLatestPromise.hook'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
app/scripts/modules/core/src/presentation/hooks/useDebouncedValue.hook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* A react hook which returns a debounced value | ||
* | ||
* @param value the immediate value | ||
* @param debounceMs: the debounce time, in ms | ||
*/ | ||
export function useDebouncedValue<T>(value: T, debounceMs: number): [T, boolean] { | ||
const [debouncedValue, setDebouncedValue] = useState(value); | ||
const [isDebouncing, setIsDebouncing] = useState(false); | ||
useEffect(() => setIsDebouncing(value !== debouncedValue)); | ||
useEffect(() => { | ||
const id = setTimeout(() => setDebouncedValue(value), debounceMs); | ||
return () => clearTimeout(id); | ||
}, [value, debounceMs]); | ||
|
||
return [debouncedValue, isDebouncing]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters