javascript REACT -类型错误: Package 为空

kupeojn6  于 2023-01-16  发布在  Java
关注(0)|答案(2)|浏览(125)

我正在用react和JS构建一个网页,但是现在我遇到了一个错误,这个错误让我的计数器出了问题。让我构建的小工具没有始终遵循3000ms的时间间隔。下面是代码。

import React from 'react';
import './portfolio.css';

const rand = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);

  const uniqueRand = (min, max, prev) => {
    let next = prev;
    while(prev === next) next = rand(min, max);
    
    return next;
  }
  const combinations = [
    { configuration: 1, roundness: 1 },
    { configuration: 1, roundness: 2 },
    { configuration: 1, roundness: 3 },
    { configuration: 2, roundness: 2 },
    { configuration: 2, roundness: 3 }
  ];

  let prev = 0;

  
const Portfolio = () => {

  const wrapper = document.getElementById("wrapper");

  setInterval(() => {
    const index = uniqueRand(0, combinations.length - 1, prev),
          combination = combinations[index];
    
    wrapper.dataset.configuration = combination.configuration;
    wrapper.dataset.roundness = combination.roundness;
    
    prev = index;
  }, 3000);
  
  return (
    <div className='RO__portfolio' id='portfolio'>
      <div className='RO__portfolio-content' data-roundness="1" data-configuration ="1" id='wrapper'>
        <div className='RO__portfolio-content_shape'></div>
        <div className='RO__portfolio-content_shape'></div>
        <div className='RO__portfolio-content_shape'></div>
        <div className='RO__portfolio-content_shape'></div>
        <div className='RO__portfolio-content_shape'></div>
        <div className='RO__portfolio-content_shape'></div>
        <div className='RO__portfolio-content_shape'></div>
      </div>
    </div>
  )

}

export default Portfolio

React标记行中的错误:

wrapper.dataset.configuration = combination.configuration;
    wrapper.dataset.roundness = combination.roundness;

我试过将这个元素移到Portfolio元素之外,但是它给了我完全相同的错误。我错过了这里发生的事情。我是react的新手

mwyxok5s

mwyxok5s1#

试着退一步想想你的代码是做什么的。
首先,用户导航到页面,然后调用此函数组件。
代码在文档中查找id为“wrapper”的元素,但它还不存在。
稍后,您的函数返回一些JSX,导致id为“wrapper”的元素出现,现在“wrapper”出现了。
但是,由于前面的代码在“wrapper”存在之前就运行了,所以它没有存储在变量中。
您可以通过多种方式解决此问题;其他人建议使用Ref。

ttp71kqs

ttp71kqs2#

我不知道configurationroundness应该做什么...
但是要每3秒在DOM元素的dataset属性中更改它们,则需要useRefuseEffect钩子。
我建议你多读一些here
一个是保留对DOM元素的引用,正如其他人提到的那样。
第二个是保留对setInterval的引用,以避免运行多个间隔。
请尝试下面的代码:
看看它在Codesandbox上运行→打开控制台!

import React, { useEffect, useRef } from "react";
// import './portfolio.css'; // Commented out because it was not provided on SO

const rand = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);

const uniqueRand = (min, max, prev) => {
  let next = prev;
  while (prev === next) next = rand(min, max);

  return next;
};
const combinations = [
  { configuration: 1, roundness: 1 },
  { configuration: 1, roundness: 2 },
  { configuration: 1, roundness: 3 },
  { configuration: 2, roundness: 2 },
  { configuration: 2, roundness: 3 }
];

let prev = 0;

const Portfolio = () => {
  const wrapperRef = useRef();

  const interval = useRef();

  useEffect(() => {
    let wrapper = wrapperRef.current;

    if (interval.current) {
      clearInterval(interval.current);
    }

    interval.current = setInterval(() => {
      const index = uniqueRand(0, combinations.length - 1, prev),
        combination = combinations[index];

      wrapper.dataset.configuration = combination.configuration;
      wrapper.dataset.roundness = combination.roundness;

      console.log({
        configuration: wrapper.dataset.configuration,
        roundness: wrapper.dataset.roundness
      });
      prev = index;
    }, 3000);
  }, []);

  return (
    <div className="RO__portfolio" id="portfolio">
      <div
        className="RO__portfolio-content"
        data-roundness="1"
        data-configuration="1"
        id="wrapper"
        ref={wrapperRef}
      >
        <div className="RO__portfolio-content_shape"></div>
        <div className="RO__portfolio-content_shape"></div>
        <div className="RO__portfolio-content_shape"></div>
        <div className="RO__portfolio-content_shape"></div>
        <div className="RO__portfolio-content_shape"></div>
        <div className="RO__portfolio-content_shape"></div>
        <div className="RO__portfolio-content_shape"></div>
      </div>
    </div>
  );
};

export default Portfolio;

注意ref={wrapperRef}以获取DOM引用。

相关问题