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