Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

useCallback hook to fix useEffect re-render warning on function dependency

import { useState, useEffect, useCallback } from 'react';

function App() {
  const [count, setCount] = useState(1);

  const logCount = useCallback(() => {
    console.log(count);
  }, [count]);

  useEffect(() => {
    logCount();
  }, [logCount]);

  return <div className="App">foo</div>;
}
Source by typeofnan.dev #
 
PREVIOUS NEXT
Tagged: #useCallback #hook #fix #useEffect #warning #function #dependency
ADD COMMENT
Topic
Name
3+3 =