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. JavaScript 类型、原型与继承学习笔记

    目录 一.概览 二.数据类型 1. JavaScript中的数据类型 2. 什么是基本类型(Primitive Data Type) 2.1 概念 2.2 七个基本类型 2.3 基本类型封装对象 3. ...

  2. gcc选项 笔记

    gcc –E hello.c –o hello.i   使用gcc的选项"-E" 让gcc在预处理结束后停止编译过程. gcc –S hello.i –o hello.s   &q ...

  3. 高效团队的gitlab flow最佳实践

    当前git是大部分开发团队的首选版本管理工具,一个好的流程规范可以让大家有效地合作,像流水线一样有条不紊地进行团队协作. 业界包含三种flow: Git flow Github flow Gitlab ...

  4. DoTween动画插件学习

    一.简单的变量插值运算 using System.Collections; using System.Collections.Generic; using UnityEngine; using DG. ...

  5. 这次一定要记住opencv和cv2是什么及其基础用法

    opencv是一个基于BSD许可发行(也就是俗称的开源)的跨平台计算机视觉库,可以运行在Linux.Windows.Android和Mac OS上.由一系列 C 函数和少量 C++ 类构成的它轻量且高 ...

  6. Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) D. Navigation System(有向图,BFS,最短路)

    题意: n 点 m 边有向图,给出行走路径,求行走途中到路径终点最短路变化次数的最小值和最大值 . 思路 : 逆向广搜,正向模拟. #include <bits/stdc++.h> usi ...

  7. Codeforces Round #693 (Div. 3) G. Moving to the Capital (图,dp)

    题意:有一张有向图,每个点的权值为点\(1\)到该点的最短距离(每条边的长度为\(1\)),对于一条路径,这条路径上最多只能有一条边,这条边起点的权值不小于终点,现在要求每个点能到达路径上的点的最小权 ...

  8. Codeforces Round #655 (Div. 2) A. Omkar and Completion (构造)

    题意:构造一个长度为\(n\)的序列,要求所有元素总和不大于\(1000\),并且任意两项的和不等于另外一项. 题解:全构造\(1\)就好了. 代码: int t; int n; int main() ...

  9. 【原创】k8s之job和Cronjob

    1.失败任务 apiVersion: batch/v1 kind: Job metadata: name: bad spec: template: metadata: name: bad spec: ...

  10. shapefile中dbf的数据格式(转载)

    来源:http://www.clicketyclick.dk/databases/xbase/format/db2_dbf.html#DB2_DBF_NOTE_4_SOURCE Xbase: dBAS ...