Last active
April 19, 2022 06:11
-
-
Save ryanflorence/0fb9190e83d99b3d779f0a593a91f07c to your computer and use it in GitHub Desktop.
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
function useIsPendingPathname(to) { | |
let { location } = useTransition(); | |
let { pathname } = useResolvedPath(to); | |
return location?.pathname === pathname; | |
} | |
function useIsSlowTransition(ms = 300) { | |
let transition = useTransition(); | |
let [isSlow, setIsSlow] = useState(false); | |
useEffect(() => { | |
if (transition.state === "loading") { | |
let id = setTimeout(() => setIsSlow(true), ms); | |
return () => clearTimeout(id); | |
} else { | |
setIsSlow(false); | |
} | |
}, [transition, ms]); | |
return isSlow; | |
} | |
function PendingLink({ to, style, ...props }) { | |
let isSlow = useIsSlowTransition(300); | |
let isPending = useIsPendingPathname(to); | |
return ( | |
<Link | |
style={{ ...style, opacity: isPending && isSlow ? 0.33 : 1 }} | |
to={to} | |
{...props} | |
/> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ryanflorence, would this be better with
let { state } = useTransition()
and then usingstate
instead oftransition
in the effect dependencies?Maybe it doesn't make much of a difference in this case, but I tend to avoid placing objects in dependency arrays if I can – in case they don't have referential stability and because they'll take up a bit (or a lot) more memory than one of their props. Do you think that's a good general rule?