-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from BerkeAras/feature/41
Fix issues
- Loading branch information
Showing
4 changed files
with
102 additions
and
12 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
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,34 @@ | ||
import React, { useState, useEffect } from 'react' | ||
|
||
// Our hook | ||
export default function useDebounce(value, delay) { | ||
// State and setters for debounced value | ||
const [debouncedValue, setDebouncedValue] = useState(value) | ||
|
||
useEffect( | ||
() => { | ||
// Set debouncedValue to value (passed in) after the specified delay | ||
const handler = setTimeout(() => { | ||
setDebouncedValue(value) | ||
}, delay) | ||
|
||
// Return a cleanup function that will be called every time ... | ||
// ... useEffect is re-called. useEffect will only be re-called ... | ||
// ... if value changes (see the inputs array below). | ||
// This is how we prevent debouncedValue from changing if value is ... | ||
// ... changed within the delay period. Timeout gets cleared and restarted. | ||
// To put it in context, if the user is typing within our app's ... | ||
// ... search box, we don't want the debouncedValue to update until ... | ||
// ... they've stopped typing for more than 500ms. | ||
return () => { | ||
clearTimeout(handler) | ||
} | ||
}, | ||
// Only re-call effect if value changes | ||
// You could also add the "delay" var to inputs array if you ... | ||
// ... need to be able to change that dynamically. | ||
[value] | ||
) | ||
|
||
return debouncedValue | ||
} |
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
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