简体   繁体   English

React Hook React.useEffect 缺少依赖项:“loadData”。 包含它或删除依赖项数组

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

I am getting this warning in the following component in react js React Hook React.useEffect has a missing dependency: 'loadData'.我在 React js React Hook React.useEffect 的以下组件中收到此警告缺少依赖项:'loadData'。 Either include it or remove the dependency array.包括它或删除依赖项数组。 can't figure out what is the issue不知道是什么问题

const ManageCourses = (props) => {
  const [data, setData] = React.useState([]);
  const [loading, setLoading] = React.useState(false);

  React.useEffect(() => {
    loadData();
  }, [props.instructor]);

  const loadData = async () => {
    setLoading(true);
    await axios
      .get(
        "http://localhost:4000/api/instructorcourses/" +
          props.instructor.InstructorID
      )
      .then((res) => {
        setData(res.data);
        setLoading(false);
      });
  };

  return (
    <div>
      {console.log(props.instructor)}
      <Row>
        <Col span={19}></Col>
        <Col span={4}>{/*<AddBadge loadData={loadData} />*/}</Col>
        <Col span={1}></Col>
      </Row>
      <br />
      <table className="table table-striped table-sm table-bordered small">
        <thead>
          <tr>
            <th className="w-25">Badge Name</th>
            <th className="w-75">Badge Detail</th>
            <th>Action</th>
          </tr>
        </thead>
        {!loading && (
          <tbody>
            {data.map((data, index) => ({
              /*<SingleBadge data={data} key={index} loadData={loadData} />*/
            }))}
          </tbody>
        )}
      </table>
    </div>
  );
};

There are 2 possible solutions you can implement, one is move loadData function in useEffect but you won't be able to access this function outside useEffect scope:您可以实施 2 种可能的解决方案,一种是在useEffect中移动loadData function,但您将无法在 useEffect scope 之外访问此useEffect

React.useEffect(() => {
  const loadData = async () => {
    setLoading(true);
    await axios
      .get(
        "http://localhost:4000/api/instructorcourses/" +
          props.instructor.InstructorID
      )
      .then((res) => {
        setData(res.data);
        setLoading(false);
      });
    };

    loadData();
  }, [props.instructor]);

And the other is to wrap loadData in useCallback and add it in the dependencies of useEffect :另一种是将loadData包装在useCallback中,并将其添加到useEffect的依赖项中:

const loadData = React.useCallback(async () => {
    setLoading(true);
    await axios
      .get(
        "http://localhost:4000/api/instructorcourses/" +
          props.instructor.InstructorID
      )
      .then((res) => {
        setData(res.data);
        setLoading(false);
      });
  }, [props.instructor]);
React.useEffect(() => {
    loadData();
  }, [loadData]);

暂无
暂无

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

相关问题 React Hook useEffect 缺少依赖项:&#39;formData&#39;。 包括它或删除依赖项数组。 什么是依赖就是使用 - 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 useEffect 缺少依赖项:&#39;props.myObj&#39;。 包括它或删除依赖项数组 - 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 缺少依赖项:&#39;context&#39;。 包括它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'context'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:&#39;fetchTracker&#39;。 包括它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'fetchTracker'. Either include it or remove the dependency array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM