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

后端 未结 14 1436
滥情空心
滥情空心 2020-12-03 09:25

I am new to React and Now I would like to show some record in the table and now I got this error. Help me, please.

Invalid hook call. Hooks can only b

相关标签:
14条回答
  • 2020-12-03 10:04

    Caught this error: found solution.

    For some reason, there were 2 onClick attributes on my tag. Be careful with using your or somebodies' custom components, maybe some of them already have onClick attribute.

    0 讨论(0)
  • 2020-12-03 10:05

    I had this issue when I used npm link to install my local library, which I've built using cra. I found the answer here. Which literally says:

    This problem can also come up when you use npm link or an equivalent. 
    In that case, your bundler might “see” two Reacts — one in application folder
    and one in your library folder. Assuming 'myapp' and 'mylib' are sibling folders,
    one possible fix is to run 'npm link ../myapp/node_modules/react' from 'mylib'.
    This should make the library use the application’s React copy.
    

    Thus, running the command: npm link ../../libraries/core/decipher/node_modules/react from my project folder has fixed the issue.

    0 讨论(0)
  • 2020-12-03 10:05

    I have just started using hooks and I got the above warning when i was calling useEffect inside a function:

    Then I have to move the useEffect outside of the function as belows:

     const onChangeRetypePassword = async value => {
      await setRePassword(value);
    //previously useEffect was here
    
      };
    //useEffect outside of func 
    
     useEffect(() => {
      if (password !== rePassword) {
      setPasswdMismatch(true);
    
         } 
      else{
        setPasswdMismatch(false);
     }
    }, [rePassword]);
    

    Hope it will be helpful to someone !

    0 讨论(0)
  • 2020-12-03 10:06

    Yesterday, I shortened the code (just added <Provider store={store}>) and still got this invalid hook call problem. This made me suddenly realized what mistake I did: I didn't install the react-redux software in that folder.

    I had installed this software in the other project folder, so I didn't realize this one also needed it. After installing it, the error is gone.

    0 讨论(0)
  • 2020-12-03 10:10

    This error can also occur when you make the mistake of declaring useDispatch from react-redux the wrong way: when you go:
    const dispatch = useDispatch instead of:
    const dispatch = useDispatch(); (i.e remember to add the parenthesis)

    0 讨论(0)
  • 2020-12-03 10:11

    You can convert class component to hooks,but Material v4 has a withStyles HOC. https://material-ui.com/styles/basics/#higher-order-component-api Using this HOC you can keep your code unchanged.

    0 讨论(0)
提交回复
热议问题