-1

I've been trying to console.log this code to print "reviewer" however it's not showing up. I have it in a functional component. What could be wrong? I'm getting "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:"

const testfc = () => {
  console.log('tesing testing 123') //only this line prints

  const [currentRole, setCurrentRole] = React.useState('reviewer')
  React.useEffect(() => console.log(currentRole), [currentRole])

  return <></>
}
testfc()

I fixed the return but still getting an error. The same one.

Colorful Codes
  • 577
  • 4
  • 16

1 Answers1

0

First your component is not a component as @evolutionxbox pointed out.

To achieve your desired results you could do something like this:

  const testfc = () => {
    const [currentRole, setCurrentRole] = React.useState('reviewer')

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

    return (
     <></>
    );
};

export default testfc;
Chris
  • 1,091
  • 11
  • 32
  • The real question is why you would want to do that, but I respect that I don't know all the details. – Chris Sep 13 '22 at 00:20