Created
September 10, 2025 19:51
-
-
Save peterbe/51638fca47829ef255a5782f17b752a8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { useEffect, useState } from "react"; | |
type Options = { | |
delay?: number | |
} | |
/** | |
* A hook that throttles the truth. Useful when you want something to be true | |
* only if it's been true for a certain delay in milliseconds. Example use: | |
* | |
* const stillLoading = useSlowTruth(isLoading); | |
* | |
* If the value of `isLoading` quickly changes from false, to true, to false; | |
* the value of `stillLoading` will remain false the whole time. | |
* | |
* @param initialState boolean | |
* @param options | |
* @returns a single boolean that is a delayed mirror of the input, if it's true | |
*/ | |
export function useSlowTruth(initialState: boolean, { delay = 1000 } : Options) { | |
const [isTrue, setIsTrue] = useState(initialState); | |
useEffect(() => { | |
let mounted = true; | |
let timer: number | null = null; | |
if (initialState) { | |
timer = window.setTimeout(() => { | |
if (mounted) { | |
setIsTrue(true); | |
} | |
}, delay); | |
} else { | |
if (timer !== null) { | |
window.clearTimeout(timer); | |
} | |
setIsTrue(false); | |
} | |
return () => { | |
mounted = false; | |
}; | |
}, [initialState, delay]); | |
return isTrue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment