处理http请求
这是一种常见的方法,您可能已经看到了许多代码的示例,这些代码在组件中进行了http调用,更改的细节,例如使用或axios的使用或状态的管理方式
>您可能已经看到了如何将此代码重新放置为自定义钩子,但让我们再次进行此组件相对简单,您在组件中具有3个状态,以表示
整个请求有效
第一种方法:
import { useeffect, usestate } from "react";
function app() {
const [isloading, setisloading] = usestate(false);
const [error, seterror] = usestate(null);
const [data, setdata] = usestate(null);
useeffect(() => {
const fetchdata = async () => {
setisloading(true);
const response = await fetch("https://fakestoreapi.com/products");
if (!response.ok) throw new error("failed to fetch data");
const data = await response.json();
setdata(data);
setisloading(false);
};
fetchdata().catch((e) => seterror(e));
}, []);
return (
{isloading && loading...
}
{error && {error.message}
}
{data && {json.stringify(data)}}
);
}
export default app;
在钩子中分开获取逻辑
第二种方法
在这种方法中,我们在自定义挂钩中分离出fetch逻辑,在任何组件中都可以重复使用,
但是我们也降低了组件的责任,现在只需要处理国家的渲染和>
申请
>但这仍然可以改善,应用程序组件仍然需要处理提出请求的逻辑,尽管不是>
需要处理请求的状态
第二种方法:
// usefetch.tsx
import { useeffect, usestate } from "react";
type usefetchparams = {
fetcher: () => promise;
querykey: string[];
};
export function usefetch({ fetcher, querykey }: usefetchparams) {
const [isloading, setisloading] = usestate(false);
const [error, seterror] = usestate(null);
const [data, setdata] = usestate(null);
const querykeystring = json.stringify(querykey);
useeffect(() => {
const fetchdata = async () => {
setisloading(true);
const data = await fetcher();
setdata(data);
setisloading(false);
};
fetchdata().catch((e) => seterror(e));
}, [querykeystring]);
return { isloading, error, data };
}
// app.tsx
import { usefetch } from "./usefetch.tsx";
const endpoint = "https://fakestoreapi.com/products";
function app() {
const { isloading, error, data } = usefetch({
querykey: [endpoint],
fetcher: async () => {
const response = await fetch(endpoint);
if (!response.ok) throw new error("failed to fetch data");
return response.json();
},
})
return (
{isloading && loading...
}
{error && {error.message}
}
{data && {json.stringify(data)}}
);
}
export default app;
在自定义挂钩中分开请求
>现在,这就是我认为理想的责任分离。
>自定义挂钩使用fect,负责处理提出请求的逻辑;
hook productuctfindall负责制作产品申请;
应用程序组件负责处理产品渲染和请求状态
第三种方法
// useproductfindall.tsx
import { usefetch } from "./usefetch.tsx";
const endpoint = "https://fakestoreapi.com/products";
async function fetchproductfindall(params = {}) {
const searchparams = new urlsearchparams(params);
const response = await fetch(endpoint + `?${searchparams.tostring()}`);
if (!response.ok) throw new error("failed to fetch data");
return response.json();
}
export function useproductfindall(params = {}) {
return usefetch({
querykey: [endpoint, params],
fetcher: () => fetchproductfindall(params)
});
}
// App.tsx
import { useProductFindAll } from "./useProductFindAll.tsx";
function App() {
const { isLoading, error, data } = useProductFindAll({ limit: 6 })
return (
{isLoading && Loading...
}
{error && {error.message}
}
{data && {JSON.stringify(data)}}
);
}
export default App;
如果您有经验或知道react-query库,则可以看到hook usefetch非常
类似的
使用react-query的钩子,这是真的
react-want是一个库,可以提取提出申请的逻辑,并且是处理
>的绝佳替代方法
后端的后端状态。该库非常强大,并且具有许多资源,例如缓存,重复,分页
>因此,如果您要处理项目中的许多请求,我建议您查看库 >
反应
相关文章
Gomoku AI 的 Minimax 实现中胜负判断逻辑错误导致忽略防守
javascript是什么_它能用来做什么呢
javascript能做什么_有哪些意想不到的实际应用?
JavaScript机器学习应用_javascript人工智能
Brain.js 实现井字棋 AI 教程:数据训练与模型优化
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
更多
相关专题
更多
http500解决方法
http500解决方法有检查服务器日志、检查代码错误、检查服务器配置、检查文件和目录权限、检查资源不足、更新软件版本、重启服务器或寻求专业帮助等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。
380
2023.11.09
http请求415错误怎么解决
解决方法:1、检查请求头中的Content-Type;2、检查请求体中的数据格式;3、使用适当的编码格式;4、使用适当的请求方法;5、检查服务器端的支持情况。更多http请求415错误怎么解决的相关内容,可以阅读下面的文章。
413
2023.11.14
http与https有哪些区别
http与https的区别:1、协议安全性;2、连接方式;3、证书管理;4、连接状态;5、端口号;6、资源消耗;7、兼容性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。
2025
2024.08.16
Golang 性能分析与pprof调优实战
本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。
9
2026.01.22
热门下载
更多
相关下载
更多
精品课程
更多
相关推荐 /
热门推荐 /
最新课程
最新文章
更多





