简体   繁体   English

React Hook useEffect 缺少一个依赖项:'tasks'。 包括它或删除依赖数组

[英]React Hook useEffect has a missing dependency: 'tasks'. Either include it or remove the dependency array

I get data from backend and set to my state in componentdidmount but value not set after log state我从后端获取数据并在 componentdidmount 中设置为我的 state 但在日志 state 后未设置值

  const [tasks, setTasks] = useState([]);

  const getTasks = async () => {
    const getTodoInformation = {
      email: localStorage.getItem("tokenEmail"),
    };
    if (getTodoInformation.email) {
      const response = await axios.post(
        "http://localhost:9000/api/todo/get",
        getTodoInformation
      );
      setTasks(response.data.data);
    }
  };

  useEffect(() => {
    getTasks();
    console.log(tasks);
  }, []);

My tasks is empty when i log it登录时我的任务是空的

 const [tasks, setTasks] = useState([]);     
   useEffect(() => {
   setTimeout(async () => {
  const getTodoInformation = {
      email: localStorage.getItem("tokenEmail"),
    };
    if (getTodoInformation.email) {
  const response = await axios.post(
    "http://localhost:9000/api/todo/get",
    getTodoInformation
  );
  setTasks(response.data.data);
}
  }, 1000);
console.log(tasks);
 }, []);

So the title and the question itself are actually two questions.所以标题和问题本身其实是两个问题。

React Hook useEffect has a missing dependency: 'tasks'. React Hook useEffect 缺少一个依赖项:'tasks'。 Either includes it or remove the dependency array要么包含它,要么移除依赖数组

That's because you include a state (ie tasks ) in the useEffect hook.那是因为您在useEffect挂钩中包含了 state (即tasks )。 And React is basically asking you, "Do you mean run console.log(tasks) every time tasks is updated?". React 基本上是在问你,“你的意思是每次更新tasks时都运行console.log(tasks)吗?”。 Because what you are doing is run the useEffect hook once and only once.因为你正在做的是一次且只运行一次useEffect钩子。

And for your "actual" question对于您的“实际”问题

value not set after log state日志 state 后未设置值

In short, states are set in async manner in React.简而言之,状态是在 React 中以异步方式设置的。 That means tasks is not necessary immediately updated right after you call setTasks .这意味着tasks不需要在您调用setTasks后立即更新。 See @JBallin comment for details.有关详细信息,请参阅@JBallin 评论。

The main problem is that useEffect -> is a sync method, getTasks() is asynchronous, and useEffect only works once when your component mounts.主要问题是 useEffect -> 是一个同步方法, getTasks() 是异步的,并且 useEffect 只在组件挂载时起作用。 Shortly speaking, you got your data from the backend after useEffect worked.简而言之,您在 useEffect 工作后从后端获取了数据。

For example, if you will add one more useEffect例如,如果您要再添加一个 useEffect

useEffect(() => {
  console.log(tasks);
}, [tasks]);

You will see log, after your data will have changed.在您的数据发生更改后,您将看到日志。

You can use self-calling async function inside useEffect as shown here:您可以在 useEffect 中使用自调用async useEffect ,如下所示:

const [tasks, setTasks] = useState([]);

const getTasks = async () => {
  const getTodoInformation = {
    email: localStorage.getItem("tokenEmail"),
  };

  if (getTodoInformation.email) {
    const response = await axios.post(
      "http://localhost:9000/api/todo/get",
      getTodoInformation
    );

    return response.data.data;
  }
};

useEffect(() => {
  (async () => {
    const tasks = await getTasks();

    setTasks(tasks);
  })();

  console.log(tasks);
}, [tasks]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Hook useEffect 缺少依赖项:'formData'。 包括它或删除依赖项数组。 什么是依赖就是使用 - React Hook useEffect has a missing dependency: 'formData'. Either include it or remove the dependency array. what is dependency is use React Hook useEffect 缺少一个依赖项:'handleLogout'。 要么包含它,要么移除依赖数组 react - React Hook useEffect has a missing dependency: 'handleLogout'. Either include it or remove the dependency array react React Hook React.useEffect 缺少依赖项:“loadData”。 包含它或删除依赖项数组 - React Hook React.useEffect has a missing dependency: 'loadData'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:'props.myObj'。 包括它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'props.myObj'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:'evaluate'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'evaluate'. Either include it or remove the dependency array 第 31:7 行:React Hook useEffect 缺少依赖项:'url'。 包括它还是删除依赖数组? - Line 31:7: React Hook useEffect has a missing dependency: 'url'. Either include it or remove the dependency array? React Hook useEffect 缺少一个依赖项:'reduxArray'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'reduxArray'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:'data'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'data'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:“match.params.roomid”。 包含它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'match.params.roomid'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:'context'。 包括它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'context'. Either include it or remove the dependency array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM