简体   繁体   English

使用 useEffect 和 useParams React Hook 时如何修复缺少的依赖警告

[英]How to fix missing dependency warning when using useEffect and useParams React Hook

import React from 'react'
import { useParams, useEffect, useState } from 'react'
import axios from "axios";
import './App.css';
const Todo = () => {
  const [todoDetails, setTodoDetails] = useState();
  const { id } = useParams();
  useEffect(() => {
// I wanted to fetch the data for the specific id from the jsonPlaceholder url to practice 

    axios
      .get(`https://jsonplaceholder.typicode.com/todos/${id}`)
      .then((res) => {
        const responseTodo = res.data;
        setTodoDetails(responseTodo);
      });

  }, [])//the console said the error is here but i don't know what to do 
// the error is "  Line 17:6:  React Hook useEffect has a missing dependency: 'id'. Either include it or remove the dependency array  react-hooks/exhaustive-deps"
  const { id: todoId, userId, title, completed } = todoDetails || {}
  return (
    <div>{`this is the todoes componets and the id is  ${todoId} , ${userId}, ${title}, ${completed}`}</div>
  )
}
export default Todo;

**I'm very new in the developer world i just started learning JS. **我是开发人员世界的新手,我刚开始学习 JS。 I was asked to do a project using react js any tips would really help me out **我被要求使用 react js 做一个项目,任何提示都会真正帮助我 **

The useEffect uses a dependency array as the second argument to watch changes, so basically if you leave the dependency array empty the useEffect will only run once when the component mounts. useEffect 使用依赖数组作为第二个参数来监视更改,因此基本上如果您将依赖数组留空,则 useEffect 只会在组件挂载时运行一次。

If you add a property or more in the dependency array it will run everytime those values change.如果您在依赖数组中添加一个或更多属性,它将在每次这些值更改时运行。

In this case your useEffect is using the id to make the api call but I'll only run once, the warning is telling you that, so if the id prop changes the useEffect won't run在这种情况下,您的 useEffect 正在使用 id 进行 api 调用,但我只会运行一次,警告会告诉您这一点,因此如果 id 道具更改,useEffect 将不会运行

Add this if you want the useEffect to be run everytime the id changes:如果您希望每次 id 更改时都运行 useEffect,请添加以下内容:

useEffect(() => {
  // Rest of the code.  
  // Adding the id here will make this effect to run everytime the id changes.
  }, [id])

It's about COMPONENTDIDUPDATE in react hooks, you can learn more about state and lifecicle concept in https://reactjs.org/docs/state-and-lifecycle.html .这是关于 react hooks 中的 COMPONENTDIDUPDATE,您可以在https://reactjs.org/docs/state-and-lifecycle.ZFC35FDC70D52C69D2698883A中了解有关 state 和生命周期概念的更多信息Your code must be like this:您的代码必须是这样的:

useEffect(() => {
    axios
      .get(`https://jsonplaceholder.typicode.com/todos/${id}`)
      .then((res) => {
        const responseTodo = res.data;
        setTodoDetails(responseTodo);
      });

  }, [id])

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

相关问题 使用 useEffect Hook 时如何修复缺少的依赖警告 - How to fix missing dependency warning when using useEffect Hook 使用 useEffect React Hook 时如何修复缺少依赖项警告? 我正在使用 nextjs 应用程序 - How to fix missing dependency warning when using useEffect React Hook? I am using nextjs app 如何解决此警告:“React Hook useEffect 缺少依赖项:'history'”? - How to fix this warning: “React Hook useEffect has a missing dependency: 'history'”? 警告:React Hook useEffect 缺少依赖项 - warning: React Hook useEffect has a missing dependency 修复 React Hook useEffect 缺少依赖项 - Fix React Hook useEffect has a missing dependency React Hook useEffect 缺少依赖项:&#39;notes&#39;,如何修复? - React Hook useEffect has a missing dependency: 'notes', how to fix it? &#39;React Hook useEffect has an missing dependency&#39; 警告功能 - 'React Hook useEffect has a missing dependency' warning with function React Hook useEffect 缺少依赖项 - missing dependency for React Hook useEffect 如何使用 useEffect 修复 React 应用程序中缺少的依赖项? - How to fix missing dependency in React applications using useEffect? 如何修复 React Redux 和 React Hook useEffect 缺少依赖项:'dispatch' - How to fix React Redux and React Hook useEffect has a missing dependency: 'dispatch'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM