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. Redis主从、哨兵模式的搭建

    壹.Redis主从分离 准备三个redis配置文件(redis.conf),分别修改为redis6380.conf.redis6381.conf.redis6382.conf 一.配置Master 1 ...

  2. SpringMVC听课笔记(十一:国际化)

    1. 关于国际化 -- 在页面上根据浏览器的语言设置情况对文本(不是内容),时间,数值进行本地化处理 使用JSTL的fmt标签 -- 可以在bean中获取国际化资源文件 Locale对应的消息 在be ...

  3. ModelArts 与HiLens Kit联合开发丨行人社交距离风险提示Demo

    摘要:本Demo使用YOLOv3_Resnet18模型来检测的视频流中的行人,获取行人坐标(即图中蓝色方框),然后计算所有检测到的人之间的相互"距离". 前情提要 听到行人社交距离 ...

  4. (十二)整合 Shiro 框架,实现用户权限管理

    整合 Shiro 框架,实现用户权限管理 1.Shiro简介 1.1 基础概念 1.2 核心角色 1.3 核心理念 2.SpringBoot整合Shiro 2.1 核心依赖 2.2 Shiro核心配置 ...

  5. Spring Cloud Config、Apollo、Nacos配置中心选型及对比

    Spring Cloud Config.Apollo.Nacos配置中心选型及对比 1.Nacos 1.1 Nacos主要提供以下四大功能 2.Spring Cloud Config 3.Apollo ...

  6. centos7系统忘记root密码

    第一步:开启服务器,在如下界面键入"e"进入编辑 第二步:找到以linux16开头的一行,在本行行尾键入"rd.break" 第三步:输入完成后,按" ...

  7. docker(1)下载安装for mac

    前言 Docker 提供轻量的虚拟化,你能够从Docker获得一个额外抽象层,你能够在单台机器上运行多个Docker微容器,而每个微容器里都有一个微服务或独立应用,例如你可以将Tomcat运行在一个D ...

  8. Pytest(14)pytest.ini配置文件

    前言 pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行 查看pytest.ini的配置选项 pytest -h 找到以下 ...

  9. 一次小模块的使用过程-LC12S无线模块介绍

    前言: 最近帮人做了个小设备,使用了无线模块.触摸芯片,主要功能就是把触摸按键的信号无线传到控制继电器输出,MCU是STM8系列的芯片,其中使用过程中调试无线模块LC21S觉得挺好用的,就写了这篇文章 ...

  10. Snapshot查询所有快照

    今天使用snapshot list这个命令时查询出了所有的表,没注意下面报错: NoMethodError:undefined method '-@' for #<Array:0x54326e9 ...