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

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

毒舌时刻

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

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

为什么你需要组件库

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

反面教材

// 反面教材:手动编写组件 // 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

OpenClaw配置飞书教程,一句话就能让 AI 帮你干活的神器(0306最新)

OpenClaw配置飞书教程,一句话就能让 AI 帮你干活的神器(0306最新)

OpenClaw 是什么?一句话就能让 AI 帮你干活的神器 OpenClaw 是一款开源的个人 AI Agent 系统,装在你的电脑或服务器上,就像有了个 24 小时待命的 AI 助手。 为什么要在飞书里用 OpenClaw? 你说一句话,它就能伸出"钳子",直接在飞书里帮你把活儿干了! 飞书刚推出了 OpenClaw 官方插件,能让你的 OpenClaw 以你的身份调用飞书的各种能力:读群聊、看文档、写文档、改文档、发消息、约日程、建多维表格……基本上你能在飞书做的事,它都能帮你做。 为什么选飞书而不是 Telegram? * 飞书是国内平台,中文界面、中文文档、中文客服,上手快 * 国内 OpenClaw 用户大多数都接入了飞书,生态更成熟

Chatbox AI 实测 | 多功能助攻 + 极致高效—横空出世的全平台AI助手!

Chatbox AI 实测 | 多功能助攻 + 极致高效—横空出世的全平台AI助手!

声明:本文是我在探索Chatbox AI应用过程中的技术实践分享,不涉及任何平台推广意图,仅为记录和交流经验 目录 一、Chatbox AI的介绍 1.1 介绍 1.2 安装教程详解 二、Chatbox AI产品实用功能详细测评 2.1 AI 驱动的图像生成(Image Creator) 2.2 代码神器:生成与预览(Software Developer) 系统架构 后端实现 (Golang) 前端实现 (Angular) 部署方案 扩展功能建议 2.3 图表制作功能 2.4 各种智能体随心用  与文档和图片聊天 实时联网搜索与查询 LaTeX 和 Markdown 渲染 三、

AI Agent 面试八股文100问:大模型智能体高频考点全解析(附分类指南和简历模板)

AI Agent 面试八股文100问:大模型智能体高频考点全解析(附分类指南和简历模板)

AI Agent 面试八股文100问:大模型智能体高频考点全解析(附分类指南和简历模板) 如果你对学成归来的简历没有概念,可以看看以下的模板先,毕竟先看清眼前的路,比奔跑更重要: 最终的AI Agent简历模板,点我跳转! 适用人群:LLM Agent、RAG、AutoGPT、LangChain、Function Calling 等方向的求职者与开发者 随着大模型技术的飞速演进,AI Agent(智能体) 已成为工业界和学术界共同关注的焦点。无论是 AutoGPT、LangChain 还是 LlamaIndex,背后都离不开对 Agent 架构、推理机制、工具调用等核心能力的深入理解。 本文系统整理了 AI Agent 方向的 100 道高频面试问题,覆盖 基础概念、架构设计、推理决策、工具调用、记忆管理、评估方法、安全对齐、

『AI辅助Skill』UI-UX-Pro-Max Skill完全指南:让开发者秒变UI设计师

📣读完这篇文章里你能收获到 1. 📁 理解UI-UX-Pro-Max Skill的核心价值和设计资源库 2. 🐍 掌握在Claude Code中安装和配置该技能的方法 3. 🌐 学会通过自然语言对话让AI自动生成专业级UI代码 4. 🖥️ 通过实战案例了解如何快速构建精美界面 文章目录 * 前言 * 一、什么是UI-UX-Pro-Max Skill? * 1.1 核心设计资源库 * 1.2 工作流程 * 1.3 技术栈支持 * 二、安装与配置 * 2.1 环境要求 * 2.2 CLI工具安装(推荐) * 2.3 手动安装 * 2.4 验证安装 * 三、使用指南与实战案例 * 3.1 基本使用方法 * 创建完整页面 * 创建特定组件 * 3.2 实战案例1: