Last active
June 14, 2019 05:39
-
-
Save troygoode/27c372e4c1c618e7bffddff52723c659 to your computer and use it in GitHub Desktop.
Hooks
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
export default function<TValue>( | |
value: TValue | null | undefined | |
): value is TValue { | |
return value !== null && value !== undefined | |
} |
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 { useMemo } from 'react' | |
import { usePromise } from 'react-hook-utils' | |
export default <T>( | |
fetchResult: () => Promise<T>, | |
setResult: (result: T) => any | |
) => { | |
const fetchAndStore = async () => { | |
return fetchResult().then((result) => { | |
setResult(result) | |
}) | |
} | |
const fetchMemo = useMemo(fetchAndStore, []) | |
const [, error, isLoading] = usePromise(fetchMemo) | |
return { | |
isLoading, | |
error, | |
refresh: fetchAndStore | |
} | |
} |
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 } from 'react' | |
export default (fn: () => void, delay: number, bind?: any[]) => { | |
useEffect(() => { | |
if (delay === 0) { | |
fn() | |
return | |
} | |
const timer = window.setTimeout(fn, delay) | |
return () => { | |
window.clearTimeout(timer) | |
} | |
}, bind || []) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment