React、TypeScript、reduce 以及 Ant Design Pro Components(antProcomponent)搭建开发环境教程
你想要的是一套完整的教程,指导如何搭建一个结合了 React、TypeScript、reduce 以及 Ant Design Pro Components(antProcomponent)的开发环境,我会一步步带你完成这个过程。
一、环境准备(前置条件)
在开始前,请确保你的电脑已安装以下工具:
- Node.js(推荐 16.x 或 18.x 版本,可通过
node -v检查) - npm 或 yarn(npm 随 Node.js 自带,yarn 可通过
npm install -g yarn安装) - 代码编辑器(推荐 VS Code)
二、搭建 React + TypeScript 基础项目
Ant Design Pro 提供了开箱即用的脚手架,能快速集成 TypeScript 和 Pro Components,比手动配置更高效:
步骤 1:创建 Ant Design Pro 项目
打开终端,执行以下命令(选择 TypeScript 模板):
# 使用官方脚手架创建项目
npm create umi@latest my-pro-app
# 或用 yarn
yarn create umi my-pro-app
执行后会出现交互式选择,按以下选项配置:
- 选择
ant-design-pro(Pro 模板) - 选择
TypeScript(语言) - 选择
simple(简易模板,适合新手) - 确认安装依赖(等待依赖下载完成)
步骤 2:安装依赖并启动项目
进入项目目录,安装依赖并启动:
cd my-pro-app
# 安装依赖
npm install
# 启动开发服务
npm run start
启动成功后,浏览器会自动打开 http://localhost:8000,能看到 Ant Design Pro 的基础页面,说明 React + TypeScript + Pro Components 环境已搭建完成。
三、集成 reduce(数组方法)的实战示例
React 中常使用 reduce 处理数组数据(比如数据聚合、筛选、格式化),结合 TypeScript 能保证类型安全,以下是结合 Pro Components(如 ProTable)的实战示例:
示例:用 reduce 处理表格数据并渲染 ProTable
- 在
src/pages下新建ReduceDemo.tsx文件,写入以下代码:
import React from 'react';
import { ProTable } from '@ant-design/pro-components';
// 定义数据类型(TypeScript 类型约束)
interface UserData {
id: number;
name: string;
department: string;
salary: number;
}
const ReduceDemo: React.FC = () => {
// 原始模拟数据
const rawData: UserData[] = [
{ id: 1, name: '张三', department: '前端', salary: 15000 },
{ id: 2, name: '李四', department: '后端', salary: 18000 },
{ id: 3, name: '王五', department: '前端', salary: 16000 },
{ id: 4, name: '赵六', department: '后端', salary: 20000 },
];
// 用 reduce 聚合:按部门分组,计算每个部门的平均工资
const departmentSalary = rawData.reduce((acc, item) => {
// 类型守卫,确保 acc 结构正确
if (!acc[item.department]) {
acc[item.department] = {
total: 0,
count: 0,
average: 0,
list: [] as UserData[],
};
}
// 累加工资和人数
acc[item.department].total += item.salary;
acc[item.department].count += 1;
// 存入当前用户
acc[item.department].list.push(item);
// 计算平均工资(保留2位小数)
acc[item.department].average = Number(
(acc[item.department].total / acc[item.department].count).toFixed(2)
);
return acc;
}, {} as Record<string, { total: number; count: number; average: number; list: UserData[] }>);
// 转换为 ProTable 可渲染的格式
const tableData = Object.entries(departmentSalary).map(([dept, data]) => ({
department: dept,
totalSalary: data.total,
personCount: data.count,
averageSalary: data.average,
}));
// ProTable 列配置
const columns = [
{
title: '部门',
dataIndex: 'department',
key: 'department',
},
{
title: '总工资',
dataIndex: 'totalSalary',
key: 'totalSalary',
},
{
title: '人数',
dataIndex: 'personCount',
key: 'personCount',
},
{
title: '平均工资',
dataIndex: 'averageSalary',
key: 'averageSalary',
},
];
return (
<div style={{ padding: 20 }}>
<h2>Reduce + ProTable 实战示例</h2>
<ProTable
dataSource={tableData}
columns={columns}
rowKey="department"
pagination={false}
title={() => '各部门工资统计(基于reduce聚合)'}
/>
</div>
);
};
export default ReduceDemo;
- 配置路由:修改
src/config/routes.ts,添加新路由:
import { IRouteProps } from '@umijs/max';
const routes: IRouteProps[] = [
{
path: '/',
redirect: '/reduce-demo',
},
{
path: '/reduce-demo',
name: 'Reduce示例',
component: './ReduceDemo',
},
];
export default routes;
- 重启项目(
npm run start),访问http://localhost:8000/reduce-demo,就能看到用reduce聚合后的数据通过 ProTable 渲染的表格。
四、关键知识点解释
- TypeScript 类型约束:
- 定义
UserData接口规范原始数据结构,避免类型错误; -
Record<string, ...>约束reduce累加器的类型,确保acc的属性和值类型符合预期。
- reduce 核心逻辑:
- 第一个参数:回调函数,接收
acc(累加器)和item(当前项),返回更新后的累加器; - 第二个参数:累加器初始值(这里是空对象,且用 TypeScript 定义了结构);
- 作用:替代循环,高效完成“分组 + 计算”的聚合操作。
- Ant Design Pro Components 优势:
-
ProTable内置了分页、筛选、排序等功能,比原生 Table 更易用; - 支持 TypeScript 类型推导,列配置的
dataIndex会自动校验是否匹配数据源。
总结
- 环境搭建核心:通过 Ant Design Pro 脚手架快速集成 React + TypeScript + Pro Components,无需手动配置复杂的 Webpack/TSConfig;
- reduce 实战:结合 TypeScript 类型约束,用 reduce 完成数组聚合(分组、计算),再通过 ProTable 渲染结果;
- 关键要点:TypeScript 接口/类型守卫保证类型安全,reduce 替代循环提升代码简洁性,Pro Components 简化业务组件开发。
如果需要扩展功能(比如接口请求、状态管理),可以基于这个基础环境,结合 Umi Max 的请求库(@umijs/max)或 React Context/Redux 进一步开发。