前端组件库:别再重复造轮子了

前端组件库:别再重复造轮子了

毒舌时刻

这组件写得跟拼凑似的,一点都不统一。

各位前端同行,咱们今天聊聊前端组件库。别告诉我你还在手动编写所有组件,那感觉就像在没有工具的情况下盖房子——能盖,但效率低得可怜。

为什么你需要组件库

最近看到一个项目,每个组件都要手动编写,样式不统一,维护困难。我就想问:你是在做组件还是在做重复劳动?

反面教材

// 反面教材:手动编写组件 // Button.jsx import React from 'react'; function Button({ children, onClick }) { return ( <button onClick={onClick} style={{ padding: '10px 20px', backgroundColor: '#007bff', color: '#fff', border: 'none', borderRadius: '4px', cursor: 'pointer' }} > {children} </button> ); } export default Button; // Input.jsx import React from 'react'; function Input({ value, onChange, placeholder }) { return ( <input type="text" value={value} onChange={onChange} placeholder={placeholder} style={{ padding: '10px', border: '1px solid #ddd', borderRadius: '4px', width: '100%' }} /> ); } export default Input; // Card.jsx import React from 'react'; function Card({ children }) { return ( <div style={{ padding: '20px', border: '1px solid #ddd', borderRadius: '4px', boxShadow: '0 2px 4px rgba(0,0,0,0.1)' }} > {children} </div> ); } export default Card; 

毒舌点评:这组件编写,就像在重复造轮子,每个组件都要手动编写样式,效率低得可怜。

正确姿势

1. Ant Design

// 正确姿势:Ant Design // 1. 安装依赖 // npm install antd // 2. 基本使用 import React from 'react'; import { Button, Input, Card, Form, Table, Modal, notification } from 'antd'; import 'antd/dist/reset.css'; function App() { const [isModalVisible, setIsModalVisible] = React.useState(false); const showModal = () => { setIsModalVisible(true); }; const handleOk = () => { setIsModalVisible(false); notification.success({ message: '成功', description: '操作成功', }); }; const handleCancel = () => { setIsModalVisible(false); }; const columns = [ { title: '姓名', dataIndex: 'name', key: 'name', }, { title: '年龄', dataIndex: 'age', key: 'age', }, { title: '操作', key: 'action', render: () => ( <Button type="primary">编辑</Button> ), }, ]; const data = [ { key: '1', name: '张三', age: 32, }, { key: '2', name: '李四', age: 42, }, ]; return ( <div style={{ padding: '20px' }}> <Card title="用户管理"> <Button type="primary" onClick={showModal} style={{ marginBottom: '20px' }}> 添加用户 </Button> <Table columns={columns} dataSource={data} /> </Card> <Modal title="添加用户" open={isModalVisible} onOk={handleOk} onCancel={handleCancel} > <Form> <Form.Item label="姓名" name="name" rules={[{ required: true, message: '请输入姓名' }]} > <Input placeholder="请输入姓名" /> </Form.Item> <Form.Item label="年龄" name="age" rules={[{ required: true, message: '请输入年龄' }]} > <Input type="number" placeholder="请输入年龄" /> </Form.Item> </Form> </Modal> </div> ); } export default App; 

2. Material UI

// 正确姿势:Material UI // 1. 安装依赖 // npm install @mui/material @emotion/react @emotion/styled // 2. 基本使用 import React from 'react'; import { Button, TextField, Card, CardContent, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, Modal, Box } from '@mui/material'; function App() { const [isModalVisible, setIsModalVisible] = React.useState(false); const [name, setName] = React.useState(''); const [age, setAge] = React.useState(''); const showModal = () => { setIsModalVisible(true); }; const handleClose = () => { setIsModalVisible(false); }; const handleSubmit = () => { console.log('提交:', { name, age }); setIsModalVisible(false); }; const rows = [ { id: 1, name: '张三', age: 32 }, { id: 2, name: '李四', age: 42 }, ]; return ( <div style={{ padding: '20px' }}> <Card sx={{ mb: 2 }}> <CardContent> <Button variant="contained" color="primary" onClick={showModal} sx={{ mb: 2 }} > 添加用户 </Button> <TableContainer component={Paper}> <Table> <TableHead> <TableRow> <TableCell>姓名</TableCell> <TableCell>年龄</TableCell> <TableCell>操作</TableCell> </TableRow> </TableHead> <TableBody> {rows.map((row) => ( <TableRow key={row.id}> <TableCell>{row.name}</TableCell> <TableCell>{row.age}</TableCell> <TableCell> <Button variant="outlined" color="primary"> 编辑 </Button> </TableCell> </TableRow> ))} </TableBody> </Table> </TableContainer> </CardContent> </Card> <Modal open={isModalVisible} onClose={handleClose} aria-labelledby="modal-modal-title" aria-describedby="modal-modal-description" > <Box sx={{ position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%, -50%)', width: 400, bgcolor: 'background.paper', border: '2px solid #000', boxShadow: 24, p: 4, }}> <h2>添加用户</h2> <TextField label="姓名" fullWidth value={name} onChange={(e) => setName(e.target.value)} /> <TextField label="年龄" type="number" fullWidth value={age} onChange={(e) => setAge(e.target.value)} /> <Box sx={{ mt: 2, display: 'flex', justifyContent: 'flex-end', gap: 1 }}> <Button onClick={handleClose}>取消</Button> <Button variant="contained" color="primary" onClick={handleSubmit}> 提交 </Button> </Box> </Box> </Modal> </div> ); } export default App; 

3. Tailwind CSS + Shadcn UI

// 正确姿势:Tailwind CSS + Shadcn UI // 1. 安装依赖 // npm install -D tailwindcss postcss autoprefixer // npx tailwindcss init -p // npx shadcn@latest init // 2. 配置 Tailwind CSS // tailwind.config.js /** @type {import('tailwindcss').Config} */ export default { content: [ "./index.html", "./src/**/*.{js,ts,jsx,tsx}", ], theme: { extend: {}, }, plugins: [], } // 3. 基本使用 import React from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Table, TableBody, TableCell, TableHead, TableRow } from '@/components/ui/table'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'; import { Label } from '@/components/ui/label'; function App() { const rows = [ { id: 1, name: '张三', age: 32 }, { id: 2, name: '李四', age: 42 }, ]; return ( <div className="p-4"> <Card className="mb-4"> <CardHeader> <CardTitle>用户管理</CardTitle> </CardHeader> <CardContent> <Dialog> <DialogTrigger asChild> <Button className="mb-4">添加用户</Button> </DialogTrigger> <DialogContent className="sm:max-w-md"> <DialogHeader> <DialogTitle>添加用户</DialogTitle> </DialogHeader> <div className="grid gap-4 py-4"> <div className="grid gap-2"> <Label htmlFor="name">姓名</Label> <Input placeholder="请输入姓名" /> </div> <div className="grid gap-2"> <Label htmlFor="age">年龄</Label> <Input type="number" placeholder="请输入年龄" /> </div> </div> <div className="flex justify-end gap-2"> <Button variant="secondary">取消</Button> <Button>提交</Button> </div> </DialogContent> </Dialog> <div className="overflow-auto"> <Table> <TableHead> <TableRow> <TableCell>姓名</TableCell> <TableCell>年龄</TableCell> <TableCell>操作</TableCell> </TableRow> </TableHead> <TableBody> {rows.map((row) => ( <TableRow key={row.id}> <TableCell>{row.name}</TableCell> <TableCell>{row.age}</TableCell> <TableCell> <Button variant="secondary">编辑</Button> </TableCell> </TableRow> ))} </TableBody> </Table> </div> </CardContent> </Card> </div> ); } export default App; 

毒舌点评:这才叫前端组件库,统一的样式,丰富的组件,开发效率高,再也不用担心重复造轮子的问题了。

Read more

格拉姆角场(Gramian Angular Field, GAF)详解

格拉姆角场(Gramian Angular Field, GAF)详解

格拉姆角场(Gramian Angular Field, GAF)是一种于2015年被提出的时间序列可视化与特征编码技术。其核心思想是将一维时间序列转换为二维图像,并在此过程中保留原始序列的时间依赖关系与数值特征。目前,GAF已在故障诊断、生物电信号分析、射频信号识别等多个领域得到广泛应用。 GAF的实质是借助极坐标变换与格拉姆矩阵的结构,将一维序列中的“时间–数值”映射为图像中的像素关联信息。生成的图像矩阵的行列索引直接对应时间顺序,使其能够兼容主流图像识别模型(如CNN),从而挖掘出时间序列中的深层特征。 一、GAF 的核心设计逻辑 传统的一维时间序列包含两类基本信息:数值大小(如振幅)和时间顺序(如信号随时间的变化趋势)。折线图等常规方法虽能展示趋势,却难以显式表达不同时刻之间的数值关联。GAF 通过以下三步逻辑实现信息的结构化编码: 1. 数值归一化:将原始序列缩放至[-1, 1]区间,消除量纲与异常值影响,为极坐标变换提供基础; 2. 极坐标转换:将时间索引映射为半径,数值大小映射为角度,建立 时间-数值 在极坐标系统中的对应关系; 3. 格拉姆矩阵构建:

RS485收发器在FPGA中的应用及注意事项

RS485收发器在FPGA中的应用及注意事项

1 前言 明确设计思路,精准定位问题,对于我们后期理解迭代工程有很大的帮助。 这就是我们常说的40%设计,20%编写和剩下的40%时间进行调试优化。 今天为大家带来的是如何解决RS485收发器使能转变引起的毛刺。 2 问题 Q1:什么时候需要用到RS485收发器? Q2:为何RS485收发器使能转变会引起毛刺? Q3:如何处理毛刺规避FPGA时序判断? 3 RS485收发器 3.1 硬件基础 3.1.1 标准收发器 RS485收发器是一类集成电路芯片,它的核心作用是在微控制器(如FPGA、MCU)的逻辑电平(如TTL电平,通常是0V/3.3V或0V/5V)与RS485差分信号之间进行双向转换。大多数RS485收发器还具备使能控制引脚(DE或RE),允许主控芯片灵活地切换其工作模式——发送或接收,从而支持半双工通信架构。 在实际应用中,微控制器输出的信号属于低电压、低电流的逻辑电平,适合短距离、高精度的内部电路通信,但无法直接用于长距离传输,

Vivado 使用教程

Vivado 使用教程

目录 一、创建工程 二、创建文件 三、编写代码 四、仿真验证 五、配置管脚 六、生成Bitstream文件并烧录 一、创建工程 1.左边创建(或打开)工程,右侧可以快速打开最近打开过的工程。 2.来到这一步,命名工程并设置工程的存放路径(这里以D触发器为例) 3.选择RTL点击next。会来到添加文件环节(可以在这里添加.v等文件,不过后面再添加是一样的)直接点击next。 4.选择芯片型号(根据开发板选,这里随便选的),完成后点next会弹出信息概要,finish完成。         二、创建文件 完成上述步骤会进入当前界面: 1.工程管理器add sourse添加(创建)设计文件,创建文件后选择Verilog语言并命名。 2.定义端口(可选),若在这定义后,

AR/VR/MR技术全景解析:从概念到应用场景的深度对比

1. 技术定义与核心要素 VR(虚拟现实)、AR(增强现实)和MR(混合现实)是当前最受关注的沉浸式技术。它们虽然都涉及虚拟与现实的交互,但核心逻辑和实现方式截然不同。 1.1 VR:完全虚拟的沉浸体验 VR通过头戴设备完全隔绝现实世界,让用户沉浸在一个由计算机生成的虚拟环境中。我曾测试过Oculus Quest 2,戴上头显的瞬间,现实中的客厅就变成了火星表面——连脚下的地面纹理都真实得让人下意识躲开裂缝。VR的三大核心要素: * 沉浸感:通过高分辨率屏幕(单眼2K以上)、120Hz刷新率和空间音频实现 * 交互性:手柄的6DoF(六自由度)追踪让我的每个手势都能精确映射到虚拟世界 * 场景构建:Unity/Unreal引擎打造的3D环境支持物理引擎碰撞检测 1.2 AR:现实世界的数字叠加 AR不像VR那样逃离现实,而是在真实环境中叠加数字信息。去年帮汽车厂商开发AR维修手册时,我们用手机扫描发动机,就能在屏幕上看到悬浮的3D拆解动画。AR的关键特征: * 实时环境理解:ARKit的平面检测能区分桌面、地面和墙壁 * 信息关联性:导航箭头会&