Skip to content

Instantly share code, notes, and snippets.

Last active January 14, 2020 08:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
// Solution 1
function useFunctionHook(fn, countInParent) {
  const [count, setCount] = React.useState(0);
  const [fnRef] = useRefValues(fn)
    () => {
    [count, setCount, fnRef, countInParent] 
 // here we have made fnRef kind of silent. 
 // It will never change after first run of the hook. as we are not changing
 // the cover
  return [setCount, fnRef.current];

function App() {
  const [countInParent, setCountInParent] = React.useState(0);
  function fn() {
    console.log("Hi bro");

  const [setCount] = useFunctionHook(fn, countInParent);

  return (
        style={{ marginTop: "50px" }}
        onClick={() => setCountInParent(countInParent + 1)}
        Change Count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment