[英]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.