React 实现请求接口封装

接口封装

一、正常接口请求

java 复制代码
useEffect(()=>{
	//获取下拉数据
	async function fetchDateWrapper() {
	try{
		const res = await 接口();
		console.log("res",res.data);
		const value = res.data.map(item ={
			return { label: item.valueName,value:valueCode }
		});
		setData(value);
		console.log("aaa",value);
	} catch (error) {
	
	} finally {
	
	}
	fetchDateWrapper();
},[])

二、antd组件请求封装

java 复制代码
 request={async (params) => {
 	const { current: pageNum, pageSize, ...otherParams } = params;
    if(otherParams?.yearMonth){
    	otherParams.beginDate=dayjs(otherParams?.yearMonth[0]).format('YYYY-MM')
        otherParams.endDate=dayjs(otherParams?.yearMonth[1]).format('YYYY-MM')
     }
    console.log('查询机构提交参数', {
        pageCurrent: pageNum,
        pageSize,
        ...otherParams,
    });
    //return []
    let {
        data,
        success,
    } = await pageQry({
        pageCurrent: pageNum,
        pageSize,
        ...otherParams,
    });
    console.log("data", data)
    console.log("data.pageInfo.records", data.pageInfo.records)
    let aaa = data.pageInfo.records
    const bbb = aaa.map((item: any, index: number) => {
        // console.log("itemitemitemitem", item)
        item.no = Number(pageSize) * (Number(pageNum) - 1) + index + 1;
        return { ...item };
    });
    setDataSourceS(bbb)
    return {
        // data: dataSource,
        success,
        total: data.pageInfo.total,
    };
 }}

出自 申创衡金科技网络有限公司

相关推荐
wayne2143 小时前
Zustand在ReactNative中的工程实践与性能优化总结
javascript·react native·react.js
阿珊和她的猫3 小时前
深入理解 React 中的 Render Props 模式
前端·react.js·状态模式
森叶3 小时前
Cookie 和 Token 的应用场景优势比较 & Cookie 不能使用的场景补充
javascript
IT_陈寒3 小时前
SpringBoot 3.0实战:10个高效开发技巧让你的启动时间减少50%
前端·人工智能·后端
im_AMBER3 小时前
前端 + agent 开发学习路线
前端·学习·agent
亿坊电商3 小时前
利于SEO优化的CMS系统都有哪些特点?
前端·数据库
lili-felicity3 小时前
React Native 鸿蒙跨平台开发:纯原生IndexBar索引栏 零依赖 快速定位列表
react native·react.js·harmonyos
juejin_cn3 小时前
使用 Codex SDK 轻松实现文字控制电脑
前端
CUYG3 小时前
Moment.js常用
前端
用户81274828151203 小时前
漏学Input知识系列之“偷”走了其他窗口的事件pilferPointers
前端