reactjs - React native - React Hook useEffect 缺少依赖项:'getAllPost'。要么包含它,要么移除依赖数组。",
问题描述
我是 react native 的新手,并尝试从中调用两个 api,useEffect
但每次都会给我这个错误React Hook useEffect has a missing dependency: 'getAllPost'. Either include it or remove the dependency array.
这是我的代码
export default function Home({navigation}) {
const [arrCat, setArrCat] = useState([]);
const [arrPost, setArrPost] = useState([]);
const [isLoading, setLoding] = useState(false);
function getAllCategory() {
setLoding(true);
let apiResponse = ApiManager.GET('category/all', [], 'GET');
apiResponse
.then(response => {
let responseJson = response[1];
let status = response[0];
setLoding(false);
let message =
responseJson.message != null
? response.message
: 'Something went wrong';
if (status === 200) {
setArrCat([...responseJson.data]);
getAllPost();
}
setTimeout(function() {
if (message != null) {
Toast.showWithGravity(message, Toast.LONG, Toast.BOTTOM);
}
}, 120);
})
.catch(error => {
console.error(error);
Toast.showWithGravity(error, Toast.LONG, Toast.BOTTOM);
setTimeout(function() {
setLoding(false);
}, 60);
});
}
function getAllPost() {
GetLocation.getCurrentPosition({
enableHighAccuracy: true,
timeout: 15000,
})
.then(location => {
console.log(location);
const dictData = {
lat: '-37.81400200-33.865143', //location.latitude,
lang: '144.9546943', //location.longitude,
record_count: '0',
};
console.log(dictData);
let apiResponse = ApiManager.POST(
'post/getRecommendedPost',
dictData,
'POST',
);
apiResponse
.then(response => {
let responseJson = response[1];
let status = response[0];
if (status === 200) {
console.log(responseJson);
setArrPost(oldValue => [...oldValue, ...responseJson.data]);
console.log(arrPost);
} else {
// console.error(responseJson);
Toast.showWithGravity(
responseJson.message,
Toast.LONG,
Toast.BOTTOM,
);
}
})
.catch(error => {
// console.error(error);
Toast.showWithGravity(error.message, Toast.LONG, Toast.BOTTOM);
// setTimeout(function() {
// setLoding(false);
// }, 60);
});
})
.catch(error => {
// const {code, message} = error;
// console.warn(code, message);
Toast.showWithGravity(error.message, Toast.LONG, Toast.BOTTOM);
});
}
useEffect(() => {
console.log('Home screen mounted');
getAllCategory();
// getAllPost();
}, []);
return ( ....)
}
解决方案
推荐阅读
- r - R中多个不同产品的线性回归
- javascript - Apexchart:Radialbar Chart Multiple 中的多个轨道颜色
- mysql - codeigniter 3 $query = $this->db->query($queri_str); 返回 0 结果
- wxwidgets - 什么可能导致 wxFrame::Destroy() 不破坏任何东西?
- javascript - 有没有办法在 iOS 上的 UC 浏览器中调试页面?
- c# - 使用 JQuery AJAX 从 Controller 获取值
- php - 在 php 应用程序中使用 sleep()?
- java - 在弹性搜索中使用通配符“like”检索文档
- c# - Asp.net 核心样板 Angular 项目无法在任何浏览器中使用 VS 2017 调试 Typescript
- linux - 如何使用 GCP 负载平衡器将 HTTP 重定向到 HTTPS