简体   繁体   English

useRef 数组元素未定义 / null 以一种奇怪的方式

[英]useRef array element is undefined / null in a weird way

When I press Prev, the useRef Array is not undefined: proof of prev is not undefined当我按下 Prev 时,useRef 数组未定义: prev 的证明未定义

But, when I press Next, the useRef Array is null: proof of null when next is pressed但是,当我按下 Next 时,useRef Array 是 null:按下 next 时 null 的证明

Both buttons run the same function on click (atleast till the console.log) but give different results.两个按钮在单击时运行相同的 function(至少到 console.log),但给出不同的结果。

I've tried running them in different order and everything else that comes to mind.我尝试过以不同的顺序运行它们以及想到的所有其他内容。

Any help is appreciated.任何帮助表示赞赏。

import React, { Suspense, useRef, useEffect, useState } from "react";
import { Canvas, useFrame } from "@react-three/fiber";
import { OrbitControls } from "@react-three/drei";

import BlankPageFlip from "./BlankPageFlip";

const App = () => {
  const refs = useRef([]);

  const numPages = 10;

  const pagesArr = [];

  let currentPage = 0;

  const [playing, setPlaying] = useState(false);
  const speed = Math.PI / 1000;

  const playAnim = (dir) => {
    console.log(refs.current); <------ THIS CONSOLE LOG

    if (refs.current[currentPage]) {
      setPlaying(dir);

      if (dir) currentPage++;
      else currentPage--;

      if (currentPage < 0) currentPage = 0;
      if (currentPage === numPages) currentPage = numPages - 1;
    }
  };

  useEffect(() => {
    for (let i = 0; i < numPages; i++) {
      pagesArr.push(
        <BlankPageFlip
          key={i}
          ref={(el) => (refs.current[i] = el)}
          position={[-1, 0.02 * i, 0]}
        />
      );
    }
  });

  const Pages = () => {
    useFrame(() => {
      if (playing) {
        if (refs.current[currentPage]) {
          if (refs.current[currentPage].rotation.z < Math.PI - 0.03) {
            refs.current[currentPage].rotation.z += speed;
          }
        }
      } else {
        if (refs.current[currentPage]) {
          if (refs.current[currentPage].rotation.z > 0) {
            refs.current[currentPage].rotation.z -= speed;
          }
        }
      }
    });

    return pagesArr;
  };

  return (
    <>
      <div className="NavButtons">
        <button className="prev" onClick={() => playAnim(false)}>
          Prev
        </button>
        <button className="next" onClick={() => playAnim(true)}>
          Next
        </button>
      </div>
      <Canvas
        className="canvas"
        colorManagement
        camera={{ position: [0, 3.7, 0], fov: 60, far: 50 }}
      >
        <ambientLight intensity={2.0} />
        <Suspense fallback={null}>
          <Pages />
          <OrbitControls />
        </Suspense>
      </Canvas>
    </>
  );
};

export default App;

So apparently, useState is not something you should use while doing this.显然, useState 不是您在执行此操作时应该使用的东西。 I removed useState and now I just use it as a let bool.我删除了 useState,现在我只是将它用作 let bool。 It works as intended now.它现在按预期工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM