交叉点观察者无法更新状态

发布于 2025-02-04 04:57:26 字数 1112 浏览 4 评论 0原文

我正在尝试使用“相交观察者”创建无限滚动,但是回调功能无法更新状态。当我向下滚动时,我可以看到States的Console.Log输出,但总是相同的。

import React, { useEffect, useState, useRef } from 'react';

// Css
import './index.css';

// Components
import QuadColors from './colors-components/quad-colors';
import SearchBar from '../../components/searchBar';
export default function colors() {
  const [renderColorSets, setRenderColorSets] = useState(5);
  const containerRef = useRef();
  const footRef = useRef();

  // Intersection Observer
  useEffect(() => {
    const observer = new IntersectionObserver((entries) => {
      if (entries[0].isIntersecting) {
        setRenderColorSets(renderColorSets + 1);
        console.log(renderColorSets);
      }
    });

    observer.observe(footRef.current);
  }, []);

  return (
    <>
      <SearchBar placeholder="Color" />
      <div className="random-colors-container" ref={containerRef}>
        {[...Array(renderColorSets)].map(() => {
          return <QuadColors />;
        })}
        <div ref={footRef} />
      </div>
    </>
  );
}```

I am trying to create infinite scrolling with intersection observer, but callback function fails to update state. When i scroll down i can see states's console.log output but its always same.

import React, { useEffect, useState, useRef } from 'react';

// Css
import './index.css';

// Components
import QuadColors from './colors-components/quad-colors';
import SearchBar from '../../components/searchBar';
export default function colors() {
  const [renderColorSets, setRenderColorSets] = useState(5);
  const containerRef = useRef();
  const footRef = useRef();

  // Intersection Observer
  useEffect(() => {
    const observer = new IntersectionObserver((entries) => {
      if (entries[0].isIntersecting) {
        setRenderColorSets(renderColorSets + 1);
        console.log(renderColorSets);
      }
    });

    observer.observe(footRef.current);
  }, []);

  return (
    <>
      <SearchBar placeholder="Color" />
      <div className="random-colors-container" ref={containerRef}>
        {[...Array(renderColorSets)].map(() => {
          return <QuadColors />;
        })}
        <div ref={footRef} />
      </div>
    </>
  );
}```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葮薆情 2025-02-11 04:57:26

您应该给SETSTATE提供更新功能,而不是更新的值本身(观察者回调函数中封闭值的原因)。尝试下一步:

if (entries[0].isIntersecting) {
  setRenderColorSets((prevColorSets) => prevColorSets + 1);
  console.log(renderColorSets);
}

因此,每当您启动SetState函数时 - 您拥有状态的最新值。

You should give an update function to setState, not an updated value itself (cause of enclosured value within observer callback function). Try to do next:

if (entries[0].isIntersecting) {
  setRenderColorSets((prevColorSets) => prevColorSets + 1);
  console.log(renderColorSets);
}

Thus whenever you fire setState function - you have the latest value of state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文