javascript - React Hook useEffect 缺少依赖项:'getData'。包括它或删除依赖数组
问题描述
我收到此错误 [React Hook useEffect 缺少依赖项:'getData'。包括它或删除依赖数组]请我解决这个问题..!
import React, { useEffect, useState } from "react";
import { Link } from "react-router-dom";
const Phaltu = (props) => {
const Id = props.match.params.gamename;
// console.log(Id);
const [value, setvalue] = useState([]);
const getData = async () => {
const res = await fetch(
"http://51.143.173.5/api/developer/matchapi.php?Action=listCompetitions&EventTypeID=" +
Id
);
const response = await res.json();
setvalue(response);
// console.log(response);
};
useEffect(() => {
getData();
}, []);
return (
<div>
<ul>
{value.map((currentElement, index) => {
return (
<li key={index}>
<Link to={"/" + Id + "/" + currentElement.competition.id}>
<p>{currentElement.competition.name}</p>
</Link>
</li>
);
})}
</ul>
</div>
);
};
export default Phaltu;
解决方案
import React, { useEffect, useState } from "react";
import { Link } from "react-router-dom";
const Phaltu = (props) => {
const Id = props.match.params.gamename;
// console.log(Id);
const [value, setvalue] = useState([]);
useEffect(() => {
const getData = async () => {
const res = await fetch(
"http://51.143.173.5/api/developer/matchapi.php?Action=listCompetitions&EventTypeID=" +
Id
);
const response = await res.json();
setvalue(response);
// console.log(response);
};
getData();
}, []);
return (
<div>
<ul>
{value.map((currentElement, index) => {
return (
<li key={index}>
<Link to={"/" + Id + "/" + currentElement.competition.id}>
<p>{currentElement.competition.name}</p>
</Link>
</li>
);
})}
</ul>
</div>
);
};
将函数声明移动到 useEffect 挂钩体内
推荐阅读
- python - 使用pyspark查找每个对应列的两个数据帧上的值差异
- javascript - 如何找出 node/npm 包导出了哪些函数?
- docker - Docker 容器重启后 WebLogic 自动部署的耳朵消失了
- php - PHP scanf 与 %f 和 %lf 的行为是否不同?
- django - 我该如何划分这些字段并聚合
- javascript - Jquery 多表过滤器
- javascript - 如何使用nestfastify编写来自nestjs异常过滤器的响应
- javascript - 自定义变量传递给 JS 回调
- python - 无法再访问 GoogleColab 的额外 RAM
- c# - 从名称中获取字符串值