Skip to content

Instantly share code, notes, and snippets.

@sibelius
Last active July 29, 2020 13:42
Show Gist options
  • Save sibelius/5849788259c6847a91199f5c36c7d7f2 to your computer and use it in GitHub Desktop.
Save sibelius/5849788259c6847a91199f5c36c7d7f2 to your computer and use it in GitHub Desktop.
useState that properly handles a function value
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