Skip to content

Instantly share code, notes, and snippets.

Last active Dec 25, 2019
What would you like to do?
Function reference changes on re-render
function useFunctionHook(fn) {
  const [count, setCount] = React.useState(0);
  React.useEffect(() => {
    // some logic
    // if fn changes the hooks callback will run
  }, [count, setCount, fn]);
  return [setCount, fn];
// Moved the fn out , now fn will have the same reference across re-render
function fn() {
    // some logic

function App() {
  const [countInParent, setCountInParent] = React.useState(0);

  const [setCount] = useFunctionHook(fn);
return (
      some child component inside.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment