当前位置: 首页 > wzjs >正文

浙江 外贸网站建设seo指什么

浙江 外贸网站建设,seo指什么,做淘宝这种网站,旅游网站的长图是怎么做的呀React通用登录/注销功能实现方案(基于shadcn/ui) 一、功能需求分析二、通用功能封装1. 通用登录表单组件2. 认证Hook封装 三、功能使用示例1. 登录页面实现2. 用户菜单实现 四、路由保护实现五、方案优势 一、功能需求分析 需要实现以下核心功能&#x…

React通用登录/注销功能实现方案(基于shadcn/ui)

    • 一、功能需求分析
    • 二、通用功能封装
      • 1. 通用登录表单组件
      • 2. 认证Hook封装
    • 三、功能使用示例
      • 1. 登录页面实现
      • 2. 用户菜单实现
    • 四、路由保护实现
    • 五、方案优势

一、功能需求分析

需要实现以下核心功能:

  1. 登录表单组件
  2. 登录状态管理
  3. 用户注销功能
  4. 路由权限控制

二、通用功能封装

1. 通用登录表单组件

// lib/components/auth-form.tsx
import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { FormEvent, ReactNode } from "react"interface AuthFormProps {className?: stringtitle: stringdescription?: stringerror?: stringfields: FormField[]submitText?: stringonSubmit: (data: Record<string, string>) => voidchildren?: ReactNode
}export type FormField = {name: stringlabel: stringtype?: stringplaceholder?: stringrequired?: boolean
}export function AuthForm({className,title,description,error,fields,submitText = "Submit",onSubmit,children
}: AuthFormProps) {const handleSubmit = (e: FormEvent<HTMLFormElement>) => {e.preventDefault()const formData = new FormData(e.currentTarget)const data = Object.fromEntries(formData.entries())onSubmit(Object.fromEntries(Object.entries(data).map(([key, value]) => [key, value.toString()])))}return (<div className={cn("flex flex-col gap-6", className)}><Card><CardHeader><CardTitle>{title}</CardTitle>{description && <CardDescription>{description</CardDescription>}</CardHeader><CardContent><form onSubmit={handleSubmit}><div className="flex flex-col gap-6">{error && (<div className="text-sm font-medium text-destructive">{error}</div>)}{fields.map((field) => (<div key={field.name} className="grid gap-3"><Label htmlFor={field.name}>{field.label}</Label><Inputid={field.name}name={field.name}type={field.type || "text"}required={field.required !== false}placeholder={field.placeholder}/></div>))}<Button type="submit" className="w-full">{submitText}</Button></div></form>{children}</CardContent></Card></div>)
}

2. 认证Hook封装

// lib/hooks/use-auth.ts
import { useState } from 'react'
import { useNavigate } from 'react-router-dom'export const useAuth = () => {const [error, setError] = useState('')const navigate = useNavigate()const login = async (credentials: Record<string, string>) => {try {// 示例验证逻辑,实际替换为API调用if (credentials.username === 'admin' && credentials.password === '123456') {localStorage.setItem('isAuthenticated', 'true')navigate('/')} else {setError('Invalid credentials')}} catch (err) {setError('Login failed')}}const logout = () => {localStorage.removeItem('isAuthenticated')navigate('/login')}return { login, logout, error }
}

三、功能使用示例

1. 登录页面实现

// app/login/page.tsx
import { AuthForm } from "@/lib/components/auth-form"
import { useAuth } from "@/lib/hooks/use-auth"export default function LoginPage() {const { login, error } = useAuth()const loginFields = [{ name: "username", label: "Username", required: true },{ name: "password", label: "Password", type: "password", required: true }]return (<div className="flex h-screen items-center justify-center bg-gray-100 p-4"><div className="w-full max-w-md"><AuthFormtitle="Login to System"description="Enter your credentials to continue"fields={loginFields}onSubmit={login}error={error}submitText="Sign In"/></div></div>)
}

2. 用户菜单实现

// components/nav-user.tsx
import { useAuth } from "@/lib/hooks/use-auth"export function NavUser() {const { logout } = useAuth()return (<DropdownMenu>{/* 其他菜单项 */}<DropdownMenuItem onClick={logout}><LogOut />Log out</DropdownMenuItem></DropdownMenu>)
}

四、路由保护实现

// router.ts
import { Navigate } from 'react-router-dom'const PrivateRoute = ({ children }: { children: JSX.Element }) => {const isAuthenticated = localStorage.getItem('isAuthenticated')return isAuthenticated ? children : <Navigate to="/login" replace />
}

五、方案优势

  1. 高度可配置:表单字段、验证逻辑均可自定义
  2. 类型安全:完善的TypeScript类型定义
  3. UI解耦:业务逻辑与UI组件分离
  4. 易于扩展:支持添加注册/找回密码等衍生功能
http://www.dtcms.com/wzjs/486887.html

相关文章:

  • 做现货去哪些网站营销好呢谷歌浏览器官网下载安装
  • 网站怎么做跟踪链接台州网站制作维护
  • wordpress成品站源码sem广告投放是做什么的
  • e通网网站建设广州seo快速排名
  • 云南昆明疫情最新消息广州软件系统开发seo推广
  • 江西昌宇建设工程公司网站互联网推广软件
  • 菜鸟教程自学网北京网络seo
  • 做网站业务员提成几个点app拉新任务平台
  • 北京网站seo公司安卓优化大师老版本
  • 自己做的网站项目怎样卖推广注册app拿佣金平台
  • 国内顶尖网站设计公司怎么让关键词快速排名首页
  • 中国十大猎头公司介绍优化清理大师
  • 安徽工程建设信息网站王开林百度搜索广告投放
  • 北京做网站多少钱合理找个免费网站这么难吗
  • 网站建设毕业实践设计报告重庆seo代理计费
  • 贵州讯玛网站建设如何做一个营销方案
  • 个人可以自己做网站吗郑州网络营销
  • 温州网站设计公司哪家好济南seo整站优化厂家
  • 上海网站开发企业二级域名网站查询入口
  • 荆门网络推广哈尔滨seo和网络推广
  • wordpress分类导航关键词优化排名软件哪家好
  • 网站建设报告 商业价值网上竞价平台
  • 电子政务门户网站建设项目招标采购快速优化官网
  • 做外贸网站有哪些成品网站源码在线看
  • 专门做塑胶原料副牌网站关键字排名优化公司
  • 金坛网站建设郑州最好的建站公司
  • 襄樊网站推广网络营销logo
  • 毕业设计做网站有哪些方面网站排名软件包年
  • 多样化的网站建设公司关键词指数
  • 凡网站建设国际新闻今天最新消息