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的更多相关文章

  1. react hooks 全面转换攻略(一) react本篇之useState,useEffect

    useState 经典案例: import { useState } from 'react'; function Example() { const [count, setCount] = useS ...

  2. 浅谈connect,withRouter,history,useState,useEffect

    1.connect in umi connect 可以链接不同的组件,从而在这个组件中使用其他组件的参数,常用于获取redux中存取的值. 2.withRouter in umi withRouter ...

  3. 手写useState与useEffect

    手写useState与useEffect useState与useEffect是驱动React hooks运行的基础,useState用于管理状态,useEffect用以处理副作用,通过手写简单的us ...

  4. React中useEffect使用

    2019-08-24 07:00:00 文摘资讯 阅读数 1364  收藏 博文的原始地址     之前我们已经掌握了useState的使用,在 class 中,我们通过在构造函数中设置 this.s ...

  5. useEffect代替常用生命周期函数(三)

    在用Class制作组件时,经常会用生命周期函数,来处理一些额外的事情(副作用:和函数业务主逻辑关联不大,特定时间或事件中执行的动作,比如Ajax请求后端数据,添加登录监听和取消登录,手动修改DOM等等 ...

  6. React Hook:使用 useEffect

    React Hook:使用 useEffect 一.描述 二.需要清理的副作用 1.在 class 组件中 2.使用 effect Hook 的示例 1.useEffect 做了什么? 2.为什么在组 ...

  7. React的useEffect与useLayoutEffect执行机制剖析

    引言 useEffect和useLayoutEffect是React官方推出的两个hooks,都是用来执行副作用的钩子函数,名字类似,功能相近,唯一不同的就是执行的时机有差异,今天这篇文章主要是从这两 ...

  8. react hooks & component will unmount & useEffect & clear up

    react hooks & component will unmount & useEffect & clear up useEffect & return === u ...

  9. React中useEffect的简单使用

    学习hooks 在 React 的世界中, 组件有函数组件和类组件 UI 组件我们可以使用函数,用函数组件来展示 UI. 而对于容器组件,函数组件就显得无能为力. 我们依赖于类组件来获取数据,处理数据 ...

随机推荐

  1. 时间模块,os模块,sys模块

    时间模块 和时间有关系的我们就要用到时间模块.在使用模块之前,应该首先导入这个模块. #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time( ...

  2. loj1011愤怒的牛

    题目描述 原题来自:USACO 2005 Feb. Gold 农夫约翰建造了一座有 n 间牛舍的小屋,牛舍排在一条直线上,第 i 间牛舍在 x_i 的位置,但是约翰的 m 头牛对小屋很不满意,因此经常 ...

  3. python3 自动部署MariaDB主从复制

    master import configparser import os def config_mariadb_yum(): exists = os.path.exists('/etc/yum.rep ...

  4. Calendar 日期判断 等于 。小于。大于

    public static void main(String[] args) throws Exception { String startTime = "2012-12-12 12:45: ...

  5. 数理统计4:均匀分布的参数估计,次序统计量的分布,Beta分布

    接下来我们就对除了正态分布以外的常用参数分布族进行参数估计,具体对连续型分布有指数分布.均匀分布,对离散型分布有二项分布.泊松分布几何分布. 今天的主要内容是均匀分布的参数估计,内容比较简单,读者应尝 ...

  6. 小白搭建WAMP详细教程---mysql安装与设置

    MySQL分为安装版和解压版.为了以后MySQL出问题想重装时会出现各种不必要的麻烦,我们这里选择解压版MySQL.详细步骤如下: 一:Mysql官网下载Mysql解压版 到官网下载,网址为:http ...

  7. ElasticSearch 安装与运行

    公号:码农充电站pro 主页:https://codeshellme.github.io 本节来介绍 ES 的安装. 1,下载 ES ES 是基于 Java 语言开发的,因此,要安装 ES,首先需要有 ...

  8. B - B Silver Cow Party (最短路+转置)

    有n个农场,编号1~N,农场里奶牛将去X号农场.这N个农场之间有M条单向路(注意),通过第i条路将需要花费Ti单位时间.选择最短时间的最优路径来回一趟,花费在去的路上和返回农场的这些最优路径的最长时间 ...

  9. Codeforces Round #647 (Div. 2) D. Johnny and Contribution(BFS)

    题目链接:https://codeforces.com/contest/1362/problem/D 题意 有一个 $n$ 点 $m$ 边的图,每个结点有一个从 $1 \sim n$ 的指定数字,每个 ...

  10. 【noi 2.6_162】Post Office(DP)

    这题和"山区建小学"除了输入不同,其他都一样.(解析可见我的上一篇随笔) 但是,这次我对dis[][]加了一个优化,画一下图就可明白. 1 #include<cstdio&g ...