2

Dashboard component:

const [breadcrumbs, setBreadcrumbs] = useState<Crumb[]>([]);

const handleCrumbs = (data: Crumb[]) => {
  setBreadcrumbs(data);
};

return (
  <>
    <Breadcrumbs crumbsArray={breadcrumbs} />
    <Route
      path={`${path}/partners`}
      render={() => <Partners crumbs={handleCrumbs} />}
    />
  </>
);

Partners component:

const Partners: React.FC<any> = ({ crumbs }) => {
  useEffect(() => {
    const arr = [1, 2, 3, 4, 5];
    crumbs(arr);
  }, []);
}

There i'm getting an error:

React Hook useEffect has a missing dependency: 'crumbs'. Either include it or remove the dependency array. If 'crumbs' changes too often, find the parent component that defines it and wrap that definition in useCallback  react-hooks/exhaustive-deps

How can i refactor this with useCallback? I can just disable eslint error and everything would work as expected, but how to do that in a "right way"?

Logic behind this, is when Partners component is mounted, i'm setting breadcrumb items in the parent component - Dashboard.

UPDATE 1

Dashboard:

const handleCrumbs = useCallback((data: Crumb[]) => {
  setBreadcrumbs(data);
}, []);

Partners:

const Partners: React.FC<any> = ({ crumbs }) => {
  useEffect(() => {
    const arr = [1, 2, 3, 4, 5];
    crumbs(arr);
  });
}

1 Answer 1

3

Wrap handleCrumbs in useCallback and then it will be safe to add crumbs to the dependencies array because the reference will never change.

without useCallback it will end up in an infinite loop because on every re-render the reference of crumbs will change

Dashboard

const handleCrumbs = useCallback((data: Crumb[]) => {
  setBreadcrumbs(data);
}, []);

Partners

const Partners: React.FC<any> = ({ crumbs }) => {
  useEffect(() => {
    const arr = [1, 2, 3, 4, 5];
    crumbs(arr);
  }, [crumbs]);
}

Also, a lot of people prefer not to use React.FC and just type children explicitly if you need them, you can read more about it here

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, but your useCallback() with empty deps, would cause the same error, as useEffect with empty deps :) BTW, thanks for React.FC note, i've read before that many people aren't preferring to use it, but couldn't understand - why.
@AlexanderKim it doesn't produce the error. Have you tried the code ?
What error exactly? I have added crumbs as a dependency in useEffect
@AsafAviv, had an error in my code, now it doesn't complain about empty deps in useCallback, but i still have infinite loop with re-renders. Will update my question with what i have tried. I just forgot to add explicitly [crumbs] in useEffect deps, BUT isn't omitting deps at all automatically adds everything in the deps?
@AlexanderKim You didn't add crumbs as a dependency to useEffect, useEffect without a second argument will run on every re-render
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.