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. 而对于容器组件,函数组件就显得无能为力. 我们依赖于类组件来获取数据,处理数据 ...
随机推荐
- 【笔记】学习markdown
经过来自学长(姐?)的 嘲讽 善意提醒后,我才知道这个博客园好像 资瓷 markdown 于是我决定要认真学习markdown(绝不是因为洛谷题解又过不去了) 正常点: 由于没人教,我上网查了一下 一 ...
- 小w、小j和小z
n个月没更了,现在学的东西很难,掌握不好,不敢更! 这个题目既不超范围又足够难想,反正我没想出来,很好的题目! 我发现noi.ac上的题目很不错!!! ------------------------ ...
- Oracle数据库查询锁表及解锁
一.查询哪些表被锁以及查看锁表得会话及操作系统进程ID 其中locked_mode为锁的级别,spid为数据库所在操作系统的进程id select c.sid, c.serial#, c.userna ...
- (三)SpringBoot停止服务的方法
SpringBoot停止服务的方法 第一种:actuator 第二种:context 第三种:进程号 第四种:SpringApplication.exit() 第五种:自定义Controller Sp ...
- Docker中运行nginx
Docker中运行nginx 1.Docker中运行nginx 2.配置文件 2.1 nginx.conf 2.2 default.conf 3.docker的镜像可以挂什么卷 部分内容原文地址: C ...
- 关于Spring Boot的博客集合
掘金: 关于Spring Boot的博客集合 CSDN: Spring Boot教程 掘金: SpringBoot2 简书: Spring Boot 核心技术 天码营 Spring Data JPA: ...
- php小程序-文章发布系统(mvc框架)
php小程序-文章发布系统(mvc框架) 一 项目视图 二 项目经验 通过对mvc微型框架的实现,对mvc理论加深,有利于以后框架的学习 三 项目源码 http://files.cnblogs.com ...
- Spring 事务、异步和循环依赖有什么关系?
前言 在循环依赖中有一种循环依赖,就是自注入:自己依赖自己. 事务的自注入 在 Spring 自调用事务失效,你是怎么解决的? 有小伙伴提出可以自己注入自己来解决事务失效. 具体使用方式如下: @Sl ...
- dedecms织梦后台栏目显示文档数不为0,但点进去之后什么都没有
曾经通过sql语句直接删除过dede_addonarticle或者dede_archives或者dede_arctiny中的记录,这三个表是有关联的,如果要通过sql语句删除内容,一定要同时将这三个表 ...
- mysql高级day4
Mysql高级-day04 1. MySql中常用工具 1.1 mysql 该mysql不是指mysql服务,而是指mysql的客户端工具. 语法 : mysql [options] [databas ...