【React】(推荐项目)一个用 React 构建的 CRUD 应用程序

news/2024/9/25 10:45:45/

推荐项目:CRUD 应用示例

在本篇文章中,我想向大家推荐一个非常实用的项目:CRUD 应用示例。这个项目展示了如何使用现代技术栈创建一个基础的增删改查(CRUD)应用,非常适合用于学习和实践后端开发技能。

适用场景

通过这个示例,您可以掌握 CRUD 操作的完整流程,并了解如何将前后端整合在一起构建一个完整的应用。

无论您是后端开发新手,还是希望进一步巩固自己的开发技能,这个项目都可以作为一个很好的参考示例。

项目链接

GitHub 项目地址:https://github.com/SafdarJamal/crud-app

项目截图

  • 登录界面
    在这里插入图片描述
  • 主页
    在这里插入图片描述
  • 编辑界面

在这里插入图片描述

部分代码

  • 登录
import React, { useState } from 'react';
import Swal from 'sweetalert2';const Login = ({ setIsAuthenticated }) => {const adminEmail = 'admin@example.com';const adminPassword = 'qwerty';const [email, setEmail] = useState('admin@example.com');const [password, setPassword] = useState('qwerty');const handleLogin = e => {e.preventDefault();if (email === adminEmail && password === adminPassword) {Swal.fire({timer: 1500,showConfirmButton: false,willOpen: () => {Swal.showLoading();},willClose: () => {localStorage.setItem('is_authenticated', true);setIsAuthenticated(true);Swal.fire({icon: 'success',title: 'Successfully logged in!',showConfirmButton: false,timer: 1500,});},});} else {Swal.fire({timer: 1500,showConfirmButton: false,willOpen: () => {Swal.showLoading();},willClose: () => {Swal.fire({icon: 'error',title: 'Error!',text: 'Incorrect email or password.',showConfirmButton: true,});},});}};return (<div className="small-container"><form onSubmit={handleLogin}><h1>Admin Login</h1><label htmlFor="email">Email</label><inputid="email"type="email"name="email"placeholder="admin@example.com"value={email}onChange={e => setEmail(e.target.value)}/><label htmlFor="password">Password</label><inputid="password"type="password"name="password"placeholder="qwerty"value={password}onChange={e => setPassword(e.target.value)}/><input style={{ marginTop: '12px' }} type="submit" value="Login" /></form></div>);
};export default Login;
  • 主页
import React, { useState, useEffect } from 'react';
import Swal from 'sweetalert2';import Header from './Header';
import Table from './Table';
import Add from './Add';
import Edit from './Edit';import { employeesData } from '../../data';const Dashboard = ({ setIsAuthenticated }) => {const [employees, setEmployees] = useState(employeesData);const [selectedEmployee, setSelectedEmployee] = useState(null);const [isAdding, setIsAdding] = useState(false);const [isEditing, setIsEditing] = useState(false);useEffect(() => {const data = JSON.parse(localStorage.getItem('employees_data'));if (data !== null && Object.keys(data).length !== 0) setEmployees(data);}, []);const handleEdit = id => {const [employee] = employees.filter(employee => employee.id === id);setSelectedEmployee(employee);setIsEditing(true);};const handleDelete = id => {Swal.fire({icon: 'warning',title: 'Are you sure?',text: "You won't be able to revert this!",showCancelButton: true,confirmButtonText: 'Yes, delete it!',cancelButtonText: 'No, cancel!',}).then(result => {if (result.value) {const [employee] = employees.filter(employee => employee.id === id);Swal.fire({icon: 'success',title: 'Deleted!',text: `${employee.firstName} ${employee.lastName}'s data has been deleted.`,showConfirmButton: false,timer: 1500,});const employeesCopy = employees.filter(employee => employee.id !== id);localStorage.setItem('employees_data', JSON.stringify(employeesCopy));setEmployees(employeesCopy);}});};return (<div className="container">{!isAdding && !isEditing && (<><HeadersetIsAdding={setIsAdding}setIsAuthenticated={setIsAuthenticated}/><Tableemployees={employees}handleEdit={handleEdit}handleDelete={handleDelete}/></>)}{isAdding && (<Addemployees={employees}setEmployees={setEmployees}setIsAdding={setIsAdding}/>)}{isEditing && (<Editemployees={employees}selectedEmployee={selectedEmployee}setEmployees={setEmployees}setIsEditing={setIsEditing}/>)}</div>);
};export default Dashboard;

如果你觉得这篇文章对你有帮助,别忘了点赞和关注,更多技术干货敬请期待!


http://www.ppmy.cn/news/1530258.html

相关文章

git配置ssh免密

为了配置 Git 通过 SSH 进行免密连接&#xff0c;你需要在本地机器上生成一个 SSH 密钥对&#xff0c;并将公钥添加到你的 GitLab 账户中。以下是详细的步骤&#xff1a; 1.检查现有的 SSH 密钥 首先&#xff0c;检查你是否已经有一个可用的 SSH 密钥。 ls -al ~/.ssh如果你…

linux安装solr

Solr Downloads - Apache Solr 直接下载&#xff1a;https://dlcdn.apache.org/solr/solr/9.7.0/solr-9.7.0.tgz 这个包依赖jdk11以上版本 需要jdk1.8版本的&#xff0c;下载Index of /dist/lucene/solr/7.1.0 # 解压 tar -zxvf solr-9.7.0.tgz # 进入启动目录 cd solr-9.7…

My_string 运算符重载,My_stack

思维导图 将My_string类中的所有能重载的运算符全部进行重载 、[] 、>、<、、>、<、! 、&#xff08;可以加等一个字符串&#xff0c;也可以加等一个字符&#xff09;、输入输出(<< 、 >>) My_string my_string.h #ifndef MY_STRING_H #define MY_…

MicroPython 怎么搭建工程代码

在MicroPython中搭建工程代码可以遵循以下步骤&#xff1a; 1. 准备工作 安装MicroPython固件&#xff1a;确保已经将MicroPython烧录到ESP32开发板中。准备开发环境&#xff1a; 可以使用文本编辑器&#xff08;如VS Code、Thonny、uPyCraft等&#xff09;来编写代码。 2.…

01DSP学习-了解DSP外设-以逆变器控制为例

(由于是回忆自己简单的DSP学习过程&#xff0c;所以博客看起来有些没有章法&#xff0c;请见谅~) 上一篇博客介绍了学习DSP需要的软件和硬件准备&#xff0c;以及一个DSP的工程包含了哪些东西。我的学习方法是目的导向&#xff0c;即我需要用什么我就学什么&#xff0c;并没有…

Mac安装manim

文章目录 0.关于Manim1.官方安装方法2.使用conda2.1 下载Anaconda2.2 创建环境2.3 Required Dependencies 3.实验 今天发现Mac电脑以前安装的manim不能用了&#xff0c;索性重新装一次&#xff0c;顺便把过程记录下来。 0.关于Manim Manim 主要分为两个主要版本&#xff1a; …

【MAC】安装realsense

找到mac系统版本号&#xff0c;使用如下链接&#xff1a; https://lightbuzz.com/realsense-macos/ 用于CPP 编译安装 libusb xxx/Library/Developer/Xcode/DerivedData/libusb-cmlwxbjexygudueqsksnyutfnmry/Build/Products/Release 记住编译路径 编译安装librealsense …

C++什么时候生成默认的拷贝构造函数

1. 默认拷贝构造存在的问题 因为默认使用的是位拷贝&#xff0c;那么如果存在指针一类对象&#xff0c;那么不同对象会持有相同对象资源。 2. 什么时候触发拷贝构造函数 &#xff08;1&#xff09;用一个对象&#xff0c;当另一个对象的构造参数。 A b; A a(b); &#xff…