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

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

毒舌时刻

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

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

为什么你需要组件库

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

反面教材

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

Neo4j-Desktop2.0安装教程(更改安装路径)

Neo4j-Desktop2.0安装教程(更改安装路径)

引言        由于neo4j-desktop2.0版本是不提供安装页面(默认安装在C盘),从而让你选择安装路径的,这对于C盘内存来说是灾难性的。因此,需要手动设置安装路径。 参考文献: 1. https://zhuanlan.zhihu.com/p/1935104156433121644https://zhuanlan.zhihu.com/p/1935104156433121644 2. https://blog.ZEEKLOG.net/WMXJY/article/details/150649084 安装包下载:https://neo4j.com/deployment-center/?desktop-gdbhttps://neo4j.com/deployment-center/?desktop-gdb 1文件夹创建及环境变量设置     首先需要在C盘以外的位置先创建一个Neo4j2文件夹,再在下面创建两个文件夹:App,PROData来存放软件本体和相关数据 然后打开“高级系统设置”——“环境变量”——系统变量下方的“新建”

前端分层架构实战:DDD 与 Clean Architecture 在大型业务系统中的落地路径与项目实践

引言 在某电商后台管理系统的迭代中,我们曾陷入典型的前端业务膨胀困境:修改 “订单拦截规则” 的状态校验逻辑时,需要同时调整 5 个关联组件的代码 —— 业务逻辑散落在组件的 setup 或 methods 中,耦合严重;后续扩展至小程序端时,核心业务逻辑无法复用,需重新编写 60% 的代码;新成员接手时,需花 1 周才能理清 “拦截规则从查询到展示” 的全链路逻辑。 这些问题的核心是 “业务逻辑与技术实现的耦合”。领域驱动设计(DDD)与整洁架构(Clean Architecture) 为解决这些问题提供了思路 —— 通过分层解耦,将 “稳定的业务规则” 与 “多变的技术工具(框架、UI 组件)” 分离,让前端系统具备长期可维护性与可扩展性。 本文结合实际项目实践,详解这两种架构在前端的落地路径。 一、前端 DDD 分层架构:

cocotb平台用VCS仿Xilinx FPGA

cocotb平台用VCS仿Xilinx FPGA

文章目录 * 概要 * 建立cocotb仿真VIP库 * 调用VIP库仿Xilinx IP * 1. VIVIDO生成IP,完成设计。 * 2. 写python仿真代码 * 3、编写Makefile * 4、运行仿真,看波形 概要 本文介绍了基于cocotb框架的AXI Stream接口验证方法。主要内容包括:1)开发AXIS VIP库,实现字节级数据发送(axis_tx_byte)、随机接收(axis_rx)和总线监控(axis_monitor_byte)功能;2)以Xilinx AXIS FIFO为例,展示VIP库的调用方法,包括测试平台搭建、数据生成和自动验证机制。该方案支持LSB配置,能模拟真实硬件背压情况,适用于AXIS接口模块的功能验证。代码提供完整的仿真环境,包含时钟复位控制、参考模型和计分板等组件,详细解析完整代码和Makefile文件。 建立cocotb仿真VIP库 例如新增一个axis.

【花雕学编程】Arduino BLDC 之基于超声波与PID控制的简单跟随机器人

【花雕学编程】Arduino BLDC 之基于超声波与PID控制的简单跟随机器人

基于 Arduino 的无刷直流电机(BLDC)超声波与 PID 控制简单跟随机器人,是一个将经典自动控制理论与现代高效驱动技术相结合的典型机电一体化项目。该系统通过超声波传感器获取环境距离信息,利用 PID 算法实时解算运动指令,并由 Arduino 驱动 BLDC 电机执行,从而实现对目标物体的稳定、平滑跟随。 1、主要特点 三角测量与单发双收架构 这是实现“定向”跟随而非“盲目”避障的核心感知逻辑。 单发双收拓扑: 系统通常采用一个手持式超声波发射模块和两个安装在机器人前端左右两侧的接收模块(单发双收)。这种布局构成了一个简单的三角形测量系统。 偏差解算原理: 当目标(人)正对机器人时,左右两个接收模块测得的距离 ,系统可以精确判断目标的偏航角度,从而实现方向控制。 双环 PID 串级控制 为了实现平稳的跟随效果,系统通常采用速度环(内环)与方向环(外环)的串级 PID 控制结构。 方向环(