React Router React Reducer Hook 实现路由管理

news/2024/7/6 0:25:23 标签: react.js

一、概述

        本文将通过React Router & React Redux实现登录和授权路由功能,将会从以下三个部分入手。

二、技术实现

auth-action-reducer (redux配置)

export const Login = (username, password) => 
        ({type: 'login',username: username,password: password };
export const Logout = (username) => 
        ({type: 'logout', username: username});

export const AuthReducer = (state, action) =>{
    switch(action.type){
        case 'Login':
            const res = auth(action.username, action.password);
            if (res){
                return {...state, loggined:true, username}
            }
            return state;
        case 'Logout':
            const res = unauth(action.username);
            if (res){
                return {...state, loggined:false}
            }
            return state;
    }
}

export const initialState = {loggined: false};

auth-page.js (登录页)

export const AuthPage = () =>{
    const [state, dispatch] = useReducer(authReducer, initialState);
    
    return (
        <div>
            <h1>Login Page</h1>
            <button onClick = () => dispatch(Login())>Login</button>
        </div>
    );
    
};

welcome-page.js (首页)

 const Welcome = () =>{
    const [state, dispatch] = useReducer(authReducer,intializeState)
    return (<div>
                <h1>Home Page</h1>
                {state.loggined
                    ?<button onClick =()=> {dispatch(Logout())}>LogOut</button>
                    :'请登录'}
            </div>);
}

app.js (入口,配置路由)

const App = () => {
    
    const [state] = useReducer(authReducer, initialState);

    return (
        <ul>
            <li><Link to = '/'>Home</Link></li>
            <li><Link to = 'login'>Login</Link></li>
        <ul>
        <Router>
            <Switch>
                <Route exact path = '/' component = {Welcome} />
                <Route exact path = '/login' render = {() => {
                    state.isLoggined ? <Redirect to = '/'/>
                    :<AuthPage/> }/>
            </Switch>
        </Router>
    );
    
}


http://www.niftyadmin.cn/n/5220673.html

相关文章

跨标签页通信的8种方式(下)

跨标签页通信是指在浏览器中的不同标签页之间进行数据传递和通信的过程。在传统的Web开发中&#xff0c;每个标签页都是相互独立的&#xff0c;无法直接共享数据。然而&#xff0c;有时候我们需要在不同的标签页之间进行数据共享或者实现一些协同操作&#xff0c;这就需要使用跨…

【SA8295P 源码分析 (四)】134 - Android 侧 NFS Client 挂载 QNX NFS Server 目录不成功 问题排查方法

【SA8295P 源码分析】134 - Android 侧 NFS Client 挂载 QNX NFS Server 目录不成功 问题排查方法 一、QNX侧1. 检查镜像是否挂载成功&#xff1a;/mnt/nfs_shared_dir 目录2. 检查 /mnt/etc/exports 文件配置是否正确3. 检查 nfsd、rpcbind 两个服务程序是否在后台工作正常 二…

[AWS 考证]CSDN官方课程目录

一、亚马逊云科技简介 二、在云中计算 三、全球基础设施和可靠性 四、联网 五、存储和数据库 六、安全性 七、监控和分析 八、定价和支持 九、迁移和创新 十、云之旅 关注订阅号 CSDN 官方中文视频&#xff08;免费&#xff09;&#xff1a;点击进入 一、亚马逊云科…

NOI / 1.10编程基础之简单排序 提问05:分数线划定 c语言 结构体

描述 世博会志愿者的选拔工作正在 A 市如火如荼的进行。为了选拔最合适的人才&#xff0c;A市对所有报名的选手进行了笔试&#xff0c;笔试分数达到面试分数线的选手方可进入面试。面试分数线根据计划录取人数的150%划定&#xff0c;即如果计划录取m名志愿者&#xff0c;则面试…

C语言文件操作 | 文件分类、文件打开与关闭、文件的读写、文件状态、文件删除与重命名、文件缓冲区

欢迎关注博主 Mindtechnist 或加入【Linux C/C/Python社区】一起学习和分享Linux、C、C、Python、Matlab&#xff0c;机器人运动控制、多机器人协作&#xff0c;智能优化算法&#xff0c;滤波估计、多传感器信息融合&#xff0c;机器学习&#xff0c;人工智能等相关领域的知识和…

ArcGIS10.x系列 Python工具箱教程

ArcGIS10.x系列 Python工具箱教程 目录 1.前提 2.需要了解的资料 3.Python工具箱制作教程 4. Python工具箱具体样例代码&#xff08;DEM流域分析-河网等级矢量化&#xff09; 1.前提 如果你想自己写Python工具箱&#xff0c;那么假定你已经会ArcPy&#xff0c;如果只是自己…

【论文阅读笔记】清单

我的论文清单 记录即将阅读的论文清单&#xff0c;持续更新。 未读论文 以下是我计划阅读但尚未开始的论文列表&#xff1a; 编号方向论文标题作者发表时间发表会议/期刊计划阅读日期code1NerfNeRFMeshing: Distilling Neural Radiance Fields into Geometrically-Accurate…

c# statusStrip 显示电脑主机名、IP地址、MAC地址

控件&#xff1a; ToolStripStatusLabel 主机名&#xff1a; Dns.GetHostName() IP地址&#xff1a; Dns.GetHostAddresses(Dns.GetHostName())[0].ToString() 当前程序的版本&#xff1a; Assembly.GetExecutingAssembly().GetName().Version.ToString() 获取系统版本 …