gpt4 book ai didi

javascript - React Hooks useState 显示错误

转载 作者:行者123 更新时间:2023-11-28 16:41:52 25 4
gpt4 key购买 nike

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 hookcall. Hooks can only be called inside of the body of a functioncomponent. This could happen for one of the following reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. You might have more than one copy of React in the same app

最佳答案

试试这个:

    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>
);
};

关于javascript - React Hooks useState 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61095585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com