反应useEffect没有运行

时间:2020-11-02 04:35:44

标签: reactjs typescript react-hooks

我的useEffect正常工作,但现在不行了。我不知道为什么。 console.log显示,现在首次运行时,什么都没有执行。代码如下。它非常简单的代码,我刚开始使用功能组件。调用InvTable时,由于在表副本中显然未定义,因此在其中出现了空错误。

const App = () => {
  const [tablesOrig, setTablesOrig] = useState<any>({});
  const [tablesCopy, setTablesCopy] = useState<any>({});
  const [numOfTables, setNumOfTables] = useState<number>(-1);
  const [rowTarget, setRowTarget] = useState<string>("");

  const setEditRow = (e: React.BaseSyntheticEvent) => {
    setRowTarget(e.target.id);
    console.log("setEditRow -> rowTarget", rowTarget);
  };

   
  useEffect(() => {
    const fetchData = async () => {
      const path = "/tables.json";
      console.log("fetchData -> path", path);  // <- never executes
      const response = await fetch(path);
      const tables: any = await response.json();
      setTablesOrig(tables);
      setTablesCopy({ ...tables });
      const tableKeys = Object.keys(tablesCopy);
      setNumOfTables(tableKeys.length);

    };
    fetchData();
  }, []);

    
  return (
    <>
      <InvTable table={tablesCopy[0]} tableId={0} setEditRow={setEditRow} />
    </>
  );
};;

0 个答案:

没有答案