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

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

毒舌时刻

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

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

为什么你需要组件库

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

反面教材

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

搭建一个基于Django框架的WebApi项目

搭建一个基于Django框架的WebApi项目

让我们一起走向未来 🎓作者简介:全栈领域优质创作者 🌐个人主页:百锦再@新空间代码工作室 📞工作室:新空间代码工作室(提供各种软件服务) 💌个人邮箱:[[email protected]] 📱个人微信:15045666310 🌐网站:https://meihua150.cn/ 💡座右铭:坚持自己的坚持,不要迷失自己!要快乐 目录 * 让我们一起走向未来 * 一、创建Django项目 * 二、安装相关依赖 * 三、配置MySQL数据库 * 四、配置Redis缓存 * 五、配置JWT中间件 * 六、配置Swagger接口文档 * 七、创建示例API * 八、总结 一、创建Django项目 首先,确保你的环境中已安装Django。如果没有,可以通过以下命令安装: pip install django

WebVOWL终极指南:如何用可视化工具轻松理解复杂本体结构

WebVOWL终极指南:如何用可视化工具轻松理解复杂本体结构 【免费下载链接】WebVOWLVisualizing ontologies on the Web 项目地址: https://gitcode.com/gh_mirrors/we/WebVOWL 在语义网和知识图谱领域,本体(Ontology)是描述概念层次和关系的重要工具。然而,面对复杂的RDF和OWL文件,如何直观地理解本体结构成为了许多研究者和开发者的挑战。WebVOWL作为一款专业的Web本体可视化工具,能够将抽象的语义数据转化为易于理解的图形化表示,让复杂的本体关系一目了然。 🎯 为什么你需要WebVOWL本体可视化工具 本体可视化不仅仅是美观的图形展示,更是理解数据关系的有效手段。通过WebVOWL,你可以: * 快速发现模式:在图形界面中立即识别出本体的核心概念和重要关系 * 交互式探索:通过点击、拖拽和缩放操作深入分析特定区域 * 团队协作优化:为技术团队和非技术人员提供统一的可视化交流语言 🚀 三步快速启动WebVOWL可视化环境 环境准备与项目获取 首先确保你的系统已安装Nod

libdatachannel:轻量级C++ WebRTC库完全指南

libdatachannel:轻量级C++ WebRTC库完全指南 【免费下载链接】libdatachannelC/C++ WebRTC network library featuring Data Channels, Media Transport, and WebSockets 项目地址: https://gitcode.com/GitHub_Trending/li/libdatachannel libdatachannel是一个轻量级的C++ WebRTC网络库,提供数据通道、媒体传输和WebSocket功能。这个开源项目让开发者能够在原生应用程序和Web浏览器之间建立直接的实时通信连接,无需依赖谷歌臃肿的参考库。 什么是libdatachannel? 🤔 libdatachannel是一个独立的WebRTC实现,支持跨平台开发,包括GNU/Linux、Android、FreeBSD、macOS、iOS和Windows。它实现了W3C和IETF标准的WebRTC协议,让设备间能够进行实时点对点数据和媒体交换。 核心功能特性 ✨ WebRTC数据通道 * 基于

Windows 下 Nginx 配置全指南:前端 Vue + 后端服务一体化部署

在现代 Web 开发中,前后端分离架构已成为主流。前端使用 Vue、React 等框架构建 SPA(单页应用),后端提供 RESTful API 服务。为了在本地或生产环境中高效部署这类应用,Nginx 是一个非常优秀的轻量级 Web 服务器和反向代理工具。 本文将手把手教你如何在 Windows 系统下下载、安装并配置 Nginx,实现: * 前端 Vue 项目的静态资源托管 * 后端 API 请求的反向代理(解决跨域) * 统一入口访问(如 http://localhost) 一、准备工作 1. 系统要求 * Windows 7 / 10 / 11 / Server 系列 * 已安装 Node.js(用于构建