在 useEffect 中 React 中的钩子调用无效

时间:2021-07-04 15:40:26

标签: javascript reactjs typescript

我在 react 上创建了应用程序,并为 EventListener 使用了 useEffect 并捕获了此错误。我尝试了来自 React 站点的所有建议,但没有人帮助我。请解释我做错了什么

我检查了我的 react 版本,一切都很好,我还安装了 react-hoock eslinter 并且没有从中得到任何错误。启动游戏功能时出错

import React, { useEffect } from "react";
import BtnPlace from "./btn";
import CreateCards from "./CardProps";

import TogglePlace from "./Toggle";

const Action = () => {
  useEffect(() => {
    let cardContainer = document.getElementById("cardsContainer");
    if (cardContainer) {
      cardContainer.addEventListener("click", Clicker);
    }

    return function () {
      if (cardContainer) {
        cardContainer.removeEventListener("click", Clicker);
      }
    };
  }, []);

  const cards = [
    {
      word: "cry",
      translation: "плакать",
      image: "/img/cry.jpg",
      audioSrc: "audio/cry.mp3",
      id: "1",
    },
    {
      word: "dance",
      translation: "танцевать",
      image: "/img/dance.jpg",
      audioSrc: "audio/dance.mp3",
      id: "2",
    },
    {
      word: "dive",
      translation: "нырять",
      image: "/img/dive.jpg",
      audioSrc: "audio/dive.mp3",
      id: "3",
    },
    {
      word: "draw",
      translation: "рисовать",
      image: "/img/draw.jpg",
      audioSrc: "audio/draw.mp3",
      id: "4",
    },
    {
      word: "fish",
      translation: "ловить ",
      image: "/img/fish.jpg",
      audioSrc: "audio/fish.mp3",
      id: "5",
    },
    {
      word: "fly",
      translation: "летать",
      image: "/img/fly.jpg",
      audioSrc: "audio/fly.mp3",
      id: "6",
    },
    {
      word: "hug",
      translation: "обнимать",
      image: "/img/hug.jpg",
      audioSrc: "audio/hug.mp3",
      id: "7",
    },
    {
      word: "jump",
      translation: "прыгать",
      image: "/img/jump.jpg",
      audioSrc: "audio/jump.mp3",
      id: "8",
    },
  ];

  const cardComponent = cards.map((card) => (
    <CreateCards
      image={card.image}
      word={card.word}
      audio={card.audioSrc}
      translation={card.translation}
      key={card.id}
    />
  ));

  const flag: boolean = true;
  let i = 0;
  let audioArr: any[] = [];

  function Game() {
    for (i = 0; i < cards.length; i++) {
      audioArr.push(cards[i].audioSrc);
    }

    audioArr.sort(() => Math.random() - 0.5);

    let audio = new Audio("/" + audioArr[0]);
    audio.play();

    if (
      document.querySelectorAll(".card__rotate") &&
      document.querySelectorAll(".card__title") &&
      document.querySelectorAll(".card__iconTwo")
    ) {
      let d1 = document.querySelectorAll(".card__rotate");
      let d2 = document.querySelectorAll(".card__title");
      let d3 = document.querySelectorAll(".card__iconTwo");
      console.log(d1);
      for (let j = 0; j < d1.length; j++) {
        //@ts-ignore
        d1[j].style.display = "none";
        //@ts-ignore
        d2[j].style.display = "none";
        //@ts-ignore
        d3[j].style.margin = "28px 97px auto";
      }
    }
  }

  function Clicker(event: any) {
    let audioDat = event.target.dataset.audio;
    console.log(audioDat);
  }

  function Sound() {
    if (flag == true) {
      let audio = new Audio("/" + audioArr[i]);
      audio.play();
      i++;
    } else {
      let audio = new Audio("/" + audioArr[i]);
      audio.play();
    }
  }

  return (
    <div className="card-container" id="cardsContainer">
      <TogglePlace />
      {cardComponent}
      <div onClick={Game}>
        <div className="btn-wrapper">
          <BtnPlace />
        </div>
      </div>
    </div>
  );
};

export default Action;

this is error that i got when i start game function

1 个答案:

答案 0 :(得分:0)

你需要在useEffect“返回函数”中返回函数

试试这个:

    useEffect(() => {
       cont cardContainer =()=>{
        let cardContainer = document.getElementById('cardsContainer')
        if (cardContainer) {
            cardContainer.addEventListener('click', Clicker)
        }
        if (cardContainer) {
            cardContainer.removeEventListener('click', Clicker)
        }
       }


        return function () {
            cardContainer() 
        }
    }, [])