简体   繁体   English

未捕获的错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 在 React 我得到这个错误

[英]Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. In React I am gettinf this error

Here I am trying to put that screening function at the radius of the TagCloud Object.在这里,我试图将筛选 function 放在 TagCloud Object 的半径处。 The screening function returns different number according to the screen width.筛选 function 根据屏幕宽度返回不同的数字。 But I am getting that error.但我得到了那个错误。 Any Solution is most welcome.任何解决方案都是最受欢迎的。 :) :)

 import { useEffect, useRef } from "react"; import "../Skills/Skills.css"; import useWindowSize from "./Screen"; const Skills = () => { const shoudLog = useRef(true); useEffect(() => { if (shoudLog.current) { shoudLog.current = false; const myTags = [ "JavaScript", "CSS", ]; const TagCloud = require("TagCloud"); const container = ".content"; TagCloud(container, myTags, { radius: Screening(), maxSpeed: "normal", }); } }, []); const Screening = () => { const windowSize = useWindowSize(); if (windowSize.width > 1240) { return 250; } else if (windowSize.width < 1240 && windowSize.width > 1000) { return 200; } else if (windowSize.width < 1000 && windowSize.width > 900) { return 180; } else if (windowSize.width < 900 && windowSize.width > 750) { return 150; } else if (windowSize.width < 750 && windowSize.width > 600) { return 130; } else if (windowSize.width < 600) { return 200; } }; return ( <div className="skillsset" id="skills"> <h1 className="skills-title">Skills</h1> </div> ); }; export default Skills;
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

With the rules of hooks , you can't use hooks outside the render method, or use them conditionally.使用hooks 的规则,你不能在 render 方法之外使用 hooks,或者有条件地使用它们。

In this part:在这部分:

const Screening = () => {
  const windowSize = useWindowSize();
  // ...
}

You are using a hook inside a nested function, which is a no-no.您在嵌套的 function 中使用了一个钩子,这是一个禁忌。 You might want to do something like this:你可能想做这样的事情:

// put the hook *directly* in the render method
const windowSize = useWindowSize();

const Screening = () => {
  // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 请帮我解决这个错误 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. please help me to solve this error 无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 有任何想法吗? - Invalid hook call. Hooks can only be called inside of the body of a function component. Any ideas? 无法从body onload调用函数(未捕获引用错误:启动未定义)javascript - Can't call function from body onload (uncaught reference error: start is not defined) javascript 无法从body onload调用函数(未捕获的参考错误:未定义resetLoginForm) - Can't call function from body onload (uncaught reference error: resetLoginForm is not defined) javascript 未捕获的错误:调用成员函数 - Uncaught Error: Call to a member function jQuery调用内的变量。 但是jQuery函数调用只发生一次 - Variable inside jquery call. But jquery function call happens only once 为什么会出现“未捕获的TypeError”错误? - Why am I getting 'Uncaught TypeError' error? 我收到“未捕获的参考错误:未定义时刻”错误。 我怎样才能解决这个问题? - I am getting 'Uncaught Reference Error: moment is not defined' error. How can I fix this? 执行 function 时,我仅在 Safari 浏览器上收到错误 - I am getting an error only on Safari Browser when executing a function 不能在回调中调用 React Hook “useState” - React Hook “useState” cannot be called inside a callback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM