0

I am pulling in a shared component from an external package, owned by myself, however I am experiencing an error that is causing my app to crash with,

Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component....

This seemed to be working fine until I upgraded the React version in the primary repo from 16 to 18.

After some debugging and commenting out code in the imported library (testing with npm link), I am finding that the useEffect within is causing the issue... And I can't fathom why. The only thing I can imagine is that the imported repo is still on version 16.xxx.

Primary repo

import ImportedComponent from '@MyCompany/package-name';

const MyComponent = () => (
    <ImportedComponent>Some children</ImportedComponent>
);

Imported repo

const ImportedRepo = ({children}) => {
    useEffect(() => {
        // Some stuff in here
    }, []);

    return <>{children}</>;
};

export default ImportedRepo;
skyboyer
  • 22,209
  • 7
  • 57
  • 64
physicsboy
  • 5,656
  • 17
  • 70
  • 119
  • There are a bunch of answers to [this related question](https://stackoverflow.com/questions/56663785/invalid-hook-call-hooks-can-only-be-called-inside-of-the-body-of-a-function-com?rq=2). Maybe some of those might work for your situation? – samuei Apr 05 '23 at 13:41

0 Answers0