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

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

毒舌时刻

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

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

为什么你需要组件库

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

反面教材

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

Stable-Diffusion-v1-5-archiveWeb UI高级功能:图生图/局部重绘/蒙版编辑实操指南

Stable Diffusion v1.5 Archive Web UI 高级功能:图生图/局部重绘/蒙版编辑实操指南 1. 引言:从文生图到创意编辑 如果你已经熟悉了 Stable Diffusion v1.5 Archive 的基础文生图功能,可能会发现,仅仅依靠文字描述来生成完美的图片,有时就像在黑暗中摸索。你想要一个特定的人物姿势,但描述了半天,出来的结果总是差那么一点;或者你生成了一张不错的风景图,但天空的颜色不够理想,想单独调整一下。 这时候,就需要用到 Web UI 中更强大的“图生图”功能了。它不再是“无中生有”,而是“有中生优”。你可以上传一张参考图,让 AI 在此基础上进行二次创作、风格迁移,或者只修改图片的某个局部区域。这大大提升了创作的灵活性和可控性。 本文将带你深入探索 Stable

探索WAAPI:开启Web动画新纪元

探索WAAPI:开启Web动画新纪元

目录 一.WAAPI的诞生背景 二.WAAPI的核心组件与工作原理 2.1核心组件 2.2工作原理 三.WAAPI的显著优势 3.1性能卓越 3.2精确控制 3.3代码简洁易读 3.4兼容性与扩展性 四.WAAPI的应用场景与实践案例 4.1页面过渡动画 4.2交互式动画 4.3数据可视化动画 五.WAAPI的未来展望         在当今数字化时代,Web页面不再仅仅是静态信息的展示平台,而是逐渐演变为充满交互性和动态效果的多媒体空间。动画作为增强用户体验、传达信息的重要手段,在Web开发中扮演着愈发关键的角色。而Web Animations API(简称WAAPI)的出现,为Web动画开发带来了革命性的变化,它以其强大的功能和灵活的操控性,成为开发者手中的一把利器。 一.WAAPI的诞生背景         在WAAPI出现之前,Web开发者实现动画主要依赖CSS动画和JavaScript动画库。CSS动画虽然简单易用,但在控制动画的精确性和交互性方面存在一定局限;而传统的JavaScript动画库虽然功能丰富,

内网穿透的应用-随时随地用 OpenClaw!打造你的专属随身 AI

内网穿透的应用-随时随地用 OpenClaw!打造你的专属随身 AI

前言 如果你已经完成了 OpenClaw 的部署,却还只局限于 “在家用电脑访问”,那真的太可惜了。这款拥有 230K + 星标的神级项目,最大的亮点就是 “本地运行、数据私有”,但局域网的限制,却让它的实用性大打折扣 —— 试想一下,当你在公司加班,需要用 OpenClaw 帮忙写一段代码、分析一份报告,却因为无法访问家里的电脑而束手无策;当你外出旅行,想让 AI 生成一份旅行攻略,却只能等回到家才能操作。这样的 OpenClaw,显然没有发挥出它应有的价值。 我在使用 OpenClaw 的过程中,也曾被这个问题困扰许久。直到接触到内网穿透工具,才彻底解决了这个痛点。不同于传统的端口映射,无需修改路由器设置,无需公网 IP,只需简单几步安装配置,就能把本地的 OpenClaw 服务映射到公网。这意味着,无论你身处何地,只要有网络,手机、平板、笔记本都能轻松连接到家里的

2026年 Trae 收费模式改变 —— AI 编程“免费午餐”终结后的生存法则

2026年 Trae 收费模式改变 —— AI 编程“免费午餐”终结后的生存法则

关键词:Trae, Cursor, AI 编程成本, Token 计费, Agent 模式, 职业转型 大家好,我是飞哥!👋 2026年,AI编辑器Trae 也将收费模式改为按 Token 收费。 有些开发者开始动摇:“AI 编辑器越来越贵,是不是应该放弃使用,回归纯手写代码?” 对于用户来说,这无疑是一次涨价。但在飞哥看来,这次涨价背后释放了两个非常关键的信号: 1. AI 技术已进入稳定成熟期: 厂商不再需要通过“免费/低价补贴”来换取用户数据进行模型迭代。产品已经足够成熟,有底气接受市场真实定价的检验。 2. 倒逼用户进化,优胜劣汰: 涨价是一道筛子。它在要求用户大幅提升自己的 AI 使用水平(如 Prompt 技巧、Context 管理)。 * 低级使用者(只会问“怎么写代码”