Home > Mobile >  React - get "invalid hook call" when write (arrow) function component
React - get "invalid hook call" when write (arrow) function component

Time:11-23

got "invalid hook call" when writing function component with arrow function.

I tried to use hooks in functional component just like below.

// this will get error

const Foo = () => {
    const fooRef = useRef(null);

    useEffect(() => {
      console.log(fooRef.current);
    }, [])

    return (
      <h1>this is foo</h1>
    );
}

export default Foo;

but got error message like below.

Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app

I have been check 1~3, but still got error.

  • Related