useState & useEffect
useState & useEffect
https://overreacted.io/zh-hans/a-complete-guide-to-useeffect/
https://reactjs.org/docs/hooks-reference.html#useeffect
https://reactjs.org/docs/hooks-effect.html
antd table
antd table 修复 分页初始值 bug
import React, {
useState,
useEffect,
} from 'react';

const [current, setCurrent] = useState(1);
const [tableData, setTableData] = useState(regions);
useEffect(() => {
setCurrent(1);
let isSubscribed = true;
if(isSubscribed) {
// cancel promise
}
return () => isSubscribed = false;
}, [adcode, regionData, regionName, regions]);
useEffect(() => {
setCurrent(1);
let isSubscribed = true;
if(isSubscribed) {
getRegionName(adcode).then(setRegionName);
}
const promises = [];
regions.forEach(item => {
promises.push(
getRegionName(item.code)
.then(name => {item.name = name;})
// eslint-disable-next-line no-console
.catch(() => console.log('获取%s地名失败', item.code)),
);
});
// 后端传来部分区划代码是错误的,找不到对应地名,暂时先剔除
Promise
.all(promises)
.then(() => {
if(isSubscribed) {
return setTableData(regions.filter(({ name }) => name));
}
});
return () => isSubscribed = false;
}, [adcode, regionData, regionName, regions]);

return (
<ExportableTable
filename={filename}
title={() => (
<>
{regionName}
<KpiTips tips={tips} />
</>
)}
rowKey="code"
bordered={false}
size="small"
pagination={{
current,
pageSize: 10,
showSizeChanger: true,
pageSizeOptions: ['5', '10', '20'],
showQuickJumper: true,
showTotal: () => <span>共 {tableData.length} 条记录</span>,
}}
columns={columns}
dataSource={tableData}
defaultCurrent={1}
onChange={(p) => {
setCurrent(p.current);
}}
/>
);
export excel
import XLSX from 'xlsx';
import { Table, Button } from 'antd';
// import { exportExcel } from '@/utils/exportUtils';
import { exportExcel } from '../../utils/exportUtils';
function noop() {}
const ExportableTable = props => {
const { filename, title = noop, columns, dataSource } = props;
const _export = () => {
exportExcel({ name: filename, columns, dataSource });
};
// 追加导出按钮
const _renderTitle = () => {
return (
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<div>{title()}</div>
<Button icon="download" size="small" onClick={_export}>
导出
</Button>
</div>
);
};
return <Table {...props} title={_renderTitle} />;
};
export default ExportableTable;
const exportExcel = ({ name, columns, dataSource }) => {
const sheetName = 'sheet';
const aoa = transformAOA({ columns, dataSource });
const sheet = XLSX.utils.aoa_to_sheet(aoa);
const workbook = {
SheetNames: [sheetName],
Sheets: { [sheetName]: sheet },
};
const opts = { bookSST: false, type: 'binary' };
XLSX.writeFile(workbook, `${name}.xlsx`, opts);
};
refs
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
useState & useEffect的更多相关文章
- react hooks 全面转换攻略(一) react本篇之useState,useEffect
useState 经典案例: import { useState } from 'react'; function Example() { const [count, setCount] = useS ...
- 浅谈connect,withRouter,history,useState,useEffect
1.connect in umi connect 可以链接不同的组件,从而在这个组件中使用其他组件的参数,常用于获取redux中存取的值. 2.withRouter in umi withRouter ...
- 手写useState与useEffect
手写useState与useEffect useState与useEffect是驱动React hooks运行的基础,useState用于管理状态,useEffect用以处理副作用,通过手写简单的us ...
- React中useEffect使用
2019-08-24 07:00:00 文摘资讯 阅读数 1364 收藏 博文的原始地址 之前我们已经掌握了useState的使用,在 class 中,我们通过在构造函数中设置 this.s ...
- useEffect代替常用生命周期函数(三)
在用Class制作组件时,经常会用生命周期函数,来处理一些额外的事情(副作用:和函数业务主逻辑关联不大,特定时间或事件中执行的动作,比如Ajax请求后端数据,添加登录监听和取消登录,手动修改DOM等等 ...
- React Hook:使用 useEffect
React Hook:使用 useEffect 一.描述 二.需要清理的副作用 1.在 class 组件中 2.使用 effect Hook 的示例 1.useEffect 做了什么? 2.为什么在组 ...
- React的useEffect与useLayoutEffect执行机制剖析
引言 useEffect和useLayoutEffect是React官方推出的两个hooks,都是用来执行副作用的钩子函数,名字类似,功能相近,唯一不同的就是执行的时机有差异,今天这篇文章主要是从这两 ...
- react hooks & component will unmount & useEffect & clear up
react hooks & component will unmount & useEffect & clear up useEffect & return === u ...
- React中useEffect的简单使用
学习hooks 在 React 的世界中, 组件有函数组件和类组件 UI 组件我们可以使用函数,用函数组件来展示 UI. 而对于容器组件,函数组件就显得无能为力. 我们依赖于类组件来获取数据,处理数据 ...
随机推荐
- mdns
mdns mdns_百度百科 https://baike.baidu.com/item/mdns 在计算机网络中 , 多播DNS ( mDNS )协议将主机名解析为不包含本地名称服务器的小型网络中的I ...
- 为什么要使用 do while(0)?
两点 避免宏定义的花括号对代码的完整性造成影响 可以在指定的代码块中(do{})使用break提前跳出,避免goto.
- C++ Primer Plus读书笔记(六)分支语句和逻辑运算符
1. 以上均包含在cctype中 1 #include<cctype> 2 //#include<ctype.h> 2.文件操作 (1)头文件 1 #include<fs ...
- deepin定制deepin-terminal
一. 背景介绍 本人以前在win10上经常使用xshell来登陆服务器.xshell提供了很丰富的功能和快捷键.个人比较喜欢的包括三个功能 终端透明 双击时根据分隔符选中文字 突出显示 但是自从使用d ...
- python中字符串的翻转(方法总结)
Python翻转字符串(reverse string), 一共包含5种方法, 其中第一种最简单, 即步长为-1, 输出字符串; 方法如下 5种方法的比较: 1. 简单的步长为-1, 即字符串的翻转(常 ...
- JavaScript 、TypeScript 中的 Boolean
boolean 是 JavaScript 中一种有趣的原始数据类型.在TypeScript中,非严格模式下("strictNullChecks": false),它总共允许4个值 ...
- VS CODE一些常见配置操作(快捷键设置、C/C++的debug、代码路径配置)
总述 今天来一篇简单的操作文章吧,VSCODE是我们经常用的软件,我之前也写过关于VSCODE远程办公的一些的操作(有兴趣的朋友可以点击进去看看),今天我再稍微介绍一些我其他地方用到的一些操作 ...
- P3381 [模板] 最小费用最大流
EK + dijkstra (2246ms) 开氧气(586ms) dijkstra的势 可以处理负权 https://www.luogu.org/blog/28007/solution-p3381 ...
- poj 1696 极角排序(解题报告)
#include<iostream> #include<cmath> #include<algorithm> using namespace std; double ...
- hdu5534 Partial Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submissi ...