Last active
July 29, 2020 13:42
-
-
Save sibelius/5849788259c6847a91199f5c36c7d7f2 to your computer and use it in GitHub Desktop.
useState that properly handles a function value
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
export const useStateCallback = (callback: Function) => { | |
const [value, setValue] = useState(() => callback); | |
const setCallback = useCallback((fn: Function) => { | |
setValue(() => fn); | |
}, [setValue] | |
return [value, setCallback]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment