Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why usecallback is triggered but the dependencies are not updated?

I have a dependencies windowSize. I also have a search input field where I update the search state. if I tip something in my search state my sites get rerendert of course because I update a state. But I have a function wrapped in useCallback but the problem is, it get triggered. So why it gets triggered ? it should only triggered when the dependencie/state windowSize gets changed.

  const [searchValue, setSearchValue] = useState<string>('');

  useEffect(() => {
    gridRef.current?.recomputeGridSize();
    console.log('TRIGGER WINDOW SIZE')
  }, [windowSize]);

  const calculateColumnCount = useCallback((width: number) => {
    console.log('CALLBACK FUNC')
    return Math.floor(width / itemMinWidth);
  }, [windowSize])

  const columnCount = numColumns ?? calculateColumnCount(containerWidth);

I put only the important things in the snipped. If you want I can share the full code.

what I am doing wrong ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

one thing to keep in mind is that
useCallback Does Not Prevent Invocation so tahe useCallback hook ensures that the function references remains stable across renders as long as its dependencies (windowSize in your case) do not change.

this line

const columnCount = numColumns ?? calculateColumnCount(containerWidth);

executes whenever numColumns is falsy. This execution is independent of whether the useCallback reference changed.

the second point to keep in mind is

State Changes Cause Re-Renders When you update the searchValue state by typing in the search input field, it causes a re-render of the component. During the re-render, if numColumns is falsy, the calculateColumnCount function is invoked again.

https://react.dev/learn/updating-objects-in-state

so if your intentions are to to only call a function once

just have a useEffect function with empty dependency array , even thew the compiler is throw a small warning its ok.

in case this solution does not satisfy you , you can also use memo

const [searchValue, setSearchValue] = useState<string>('');

useEffect(() => {
  gridRef.current?.recomputeGridSize();
  console.log('TRIGGER WINDOW SIZE');
}, [windowSize]);

const calculateColumnCount = useCallback((width: number) => {
  console.log('CALLBACK FUNC');
  return Math.floor(width / itemMinWidth);
}, [windowSize]);

// Memoize the column count to avoid unnecessary recalculations
const columnCount = useMemo(() => {
  return numColumns ?? calculateColumnCount(containerWidth);
}, [numColumns, containerWidth, calculateColumnCount]);

or maybe using a more robust react redux toolkit which will help you managing state and renders only the component it store the state at but its a bit stiff curve to learn , but I truly recommend 🙂 keep coding dude let me know if that helped you , upvote if it did and mark as answer 🙂 will help me get to 900 points

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading