[英]React Hooks useState showing errors
import React, { useState, useEffect } from 'react';
import ReactScrollDetector from 'react-scroll-detector';
const DatePicker = ({ count }) => {
const [countNew, setCount] = useState(count);
const handleScrollBottom = () => {
useEffect(() => setCount((countNew = countNew + 12)));
};
const handleScrollTop = () => {
useEffect(() => setCount((countNew = countNew + 12)));
};
return (
<ReactScrollDetector
debounceTime={500}
accuracy={90}
onScrollBottom={handleScrollBottom}
onScrollTop={handleScrollTop}
>
<div style={{ minHeight: '300px', maxHeight: '300px', overflow: 'auto' }}> </div>
</ReactScrollDetector>
);
};
react-dom.development.js:55 Uncaught Invariant Violation: Invalid hook call.
react-dom.development.js:55 Uncaught Invariant Violation:无效的钩子调用。 Hooks can only be called inside of the body of a function component.
钩子只能在 function 组件的主体内部调用。 This could happen for one of the following reasons:
这可能由于以下原因之一而发生:
- You might have mismatching versions of React and the renderer (such as React DOM)
你可能有不匹配的 React 版本和渲染器(例如 React DOM)
- You might be breaking the Rules of Hooks
您可能违反了 Hooks 规则
- You might have more than one copy of React in the same app
你可能在同一个应用程序中拥有多个 React 副本
try this:尝试这个:
import React, { useState, useEffect } from "react";
import ReactScrollDetector from "react-scroll-detector";
const DatePicker = ({count}) => {
const [countNew, setCount] = useState(count)
const handleScrollBottom = () => {
setCount(countNew + 12)
}
const handleScrollTop = () => {
setCount(countNew + 12)
}
return (
<ReactScrollDetector
debounceTime={500}
accuracy={90}
onScrollBottom={handleScrollBottom}
onScrollTop={handleScrollTop}>
<div style={{ minHeight: "300px", maxHeight: "300px", overflow: "auto" }}> <div>
</ReactScrollDetector>
);
};
let your functions like this让你的功能像这样
const handleScrollBottom = () => setCount(countNew = countNew + 12)) const handleScrollTop = () => setCount(countNew = countNew + 12))
the error错误
react-dom.development.js:55 Uncaught Invariant Violation:
is because useEffect Hook needs to be outside of the functions for example是因为 useEffect Hook 需要在函数之外,例如
export default function MyComponent() { useEffect(() => { }, []); const bar = () => {}; const foo = () => {}; return <div></div>; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.