停止使用useEffect钩子引起无限循环

时间:2020-07-07 14:30:34

标签: javascript reactjs react-hooks infinite-loop

我是一个很新的反应者,我设法为一个简单的待办事项清单创建了一个API。我已经从api中提取了数据并将其显示在屏幕上。

如果我在useEffect()钩子上将依赖项数组留空,它将仅呈现一次,并且不会循环。但是,如果添加新的Todo,除非刷新,否则不会更新列表。因此,我将todos状态放入依赖项数组中,然后在添加新项时显示该新项,但是如果我查看开发工具中的network选项卡,则会无限循环地击中api。我在做什么错了?

代码如下:

App

import React, { useState, useEffect } from "react";
import Todo from "./components/Todo";
import Heading from "./components/Heading";
import NewTodoForm from "./components/NewTodoForm";

const App = () => {
  const [todos, setTodos] = useState([]);

  useEffect(() => {
    const getTodos = async () => {
      const res = await fetch("http://localhost:3001/api/todos");
      const data = await res.json();
      setTodos(data);
    };

    getTodos();
  }, []);

  return (
    <div className="container">
      <Heading todos={todos} />
      <section className="todos-container">
        <ul className="todos">
          {todos.map((todo) => (
            <Todo key={todo._id} todo={todo} />
          ))}
        </ul>
      </section>
      <section className="todo-form">
        <NewTodoForm />
      </section>
    </div>
  );
};

export default App;

标题

import React from "react";

const Heading = ({ todos }) => (
  <header>
    <h1>Todos</h1>
    <p>
      {todos.length} {todos.length === 1 ? "Item" : "Items"}
    </p>
  </header>
);

export default Heading;

Todo

import React, { useState } from "react";

const Todo = ({ todo }) => (
  <li>
    {todo.name}
    <input type="checkbox" />
  </li>
);

export default Todo;

NewTodoForm

import React, { useState } from "react";
import { Plus } from "react-feather";

const NewTodoForm = () => {
  const [formData, setFormData] = useState({
    name: "",
    completed: false,
  });

  const { name } = formData;

  const handleOnChange = (e) => {
    setFormData({
      ...formData,
      [e.target.name]: e.target.value,
    });
  };

  const handleSubmit = async (e) => {
    e.preventDefault();

    await fetch("http://localhost:3001/api/todos", {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
      },
      body: JSON.stringify(formData),
    });

    setFormData({
      name: "",
      completed: false,
    });
  };
  return (
    <form onSubmit={handleSubmit}>
      <div className="form-control">
        <Plus className="plus" />
        <input
          name="name"
          type="text"
          placeholder="Add New Item"
          onChange={handleOnChange}
          value={name}
        />
        <button>Add</button>
      </div>
    </form>
  );
};

export default NewTodoForm;

如果我注释掉所有组件并且只有App组件,则在向useEffect()挂钩的依赖项数组中添加待办事项时,它仍然会无限循环。

1 个答案:

答案 0 :(得分:2)

因此,不要在useEffect之外写函数,这样您就可以在添加待办事项之后调用该函数

示例:

  const getTodos = async () => {
      const res = await fetch("http://localhost:3001/api/todos");
      const data = await res.json();
      setTodos(data);
    };

 useEffect(() => {
    getTodos();
  }, []);

因此getTodos最初只会运行一次,并且只能在onSubmit的{​​{1}}或onClick上运行,因此,只需在onSubmit上调用Todo函数或onClick