簡體   English   中英

當沒有這樣的條件調用時,對 useEffect Hook 條件調用反應錯誤

[英]React error for useEffect Hook conditional call when there is no such conditional call

React 抱怨下面的代碼,說它useEffect被有條件地調用:

import React, { useEffect } from "react";
import ReactDOM from "react-dom";

function App() {
  const documentCounts = {};
  const invertedIndexes = {};

  for (const term of []) {
    if (!invertedIndexes[term]) continue;
    // The offending code is the loop below
    for (const arr of invertedIndexes[term]) {
      const documentIndex = arr[0];
      if (!documentCounts[documentIndex]) documentCounts[documentIndex] = 1;
      else ++documentCounts[documentIndex];
    }
  }

  useEffect(() => {});

  return (
    <div className="App">
      <h1>Hello World</h1>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render. Did you accidentally call a React Hook after an early return? react-hooks/rules-of-hooks

但對我來說似乎沒有條件調用useEffect - 循環只是修改局部變量。 有誰碰巧知道這里的問題是什么?

(無意義的變量是我將原始代碼歸結為試圖查明問題的原因)

沙盒: https : //codesandbox.io/s/friendly-diffie-z7edc

查看有關鈎子的官方文檔: 始終在 React 函數的頂層使用鈎子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM