Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

How to fix warning "function -- makes the dependencies of useEffect Hook change on every render"?

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: #How #fix #warning #dependencies #useEffect #Hook #change
ADD COMMENT
Topic
Name
7+8 =