为了账号安全,请及时绑定邮箱和手机立即绑定

react18+acro design高仿微信web版聊天

这次分享一个最新研发的react18 hooks+acro+zustand仿微信网页聊天项目ReactWebchat

图片描述

react18-webchat全部采用react18 hooks编码开发。

图片描述

技术栈

  • 编辑器:vscode
  • 技术栈:react18+vite4+react-router-dom+zustand+sass
  • 组件库:@arco-design/web-react (字节跳动react组件库)
  • 状态管理:zustand^4.4.1
  • 路由管理:react-router-dom^6.15.0
  • className拼合:clsx^2.0.0
  • 对话框组件:rdialog (基于react18 hooks自定义桌面端弹窗组件)
  • 滚动条组件:rscroll (基于react18美化系统滚动条组件)

图片描述

项目结构

采用vite4创建react18聊天项目。构建速度快,目录结构分明,搭配vite.config.js配置,效果杠杠滴。

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

图片描述

项目中使用的弹窗及滚动条组件是使用自定义组件实现功能效果。

// 引入弹窗组件
import RDialog, { rdialog } from '@/components/rdialog'

// 组件式调用
<RDialog
    visible={confirmVisible}
    title="标题信息"
    content="对话框内容信息"
    closeable
    shadeClose={false}
    zIndex="2050"
    dragOut
    maxmin
    btns={[
        {text: '取消', click: () => setConfirmVisible(false)},
        {text: '确定', click: handleInfo}
    ]}
    onClose={()=>setConfirmVisible(false)}
/>

// 函数式调用
rdialog({
    title: '标题信息',
    content: '对话框内容信息',
    closeable: true,
    shadeClose: false,
    zIndex: 2050,
    dragOut: true,
    maxmin: true,
    btns: [
        {text: '取消', click: rdialog.close()},
        {text: '确定', click: handleInfo}
    ]
})
// 引入滚动条组件
import RScroll from '@/components/rscroll'

<RScroll autohide maxHeight={100}>
    包裹需要滚动的内容块。。。
</RScroll>

main.jsx主配置

import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import '@arco-design/web-react/dist/css/arco.css'
import './style.scss'

ReactDOM.createRoot(document.getElementById('root')).render(<App />)

App.jsx模板配置

import { HashRouter } from 'react-router-dom'

// 引入useRoutes集中式路由配置文件
import Router from './router'

function App() {
    return (
        <>
            <HashRouter>
              <Router />
            </HashRouter>
        </>
    )
}

export default App

路由配置react-router-dom

/**
 * 路由配置 by YXY Q:282310962
*/

import { lazy, Suspense } from 'react'
import { useRoutes, Outlet, Navigate } from 'react-router-dom'
import { Spin } from '@arco-design/web-react'

import { authStore } from '@/store/auth'

// 引入路由页面
import Login from '@views/auth/login'
import Register from '@views/auth/register'
const Index = lazy(() => import('@views/index'))
const Contact = lazy(() => import('@views/contact'))
const Uinfo = lazy(() => import('@views/contact/uinfo'))
const NewFriend = lazy(() => import('@views/contact/newfriend'))
const Chat = lazy(() => import('@views/chat/chat'))
const ChatInfo = lazy(() => import('@views/chat/info'))
const RedPacket = lazy(() => import('@views/chat/redpacket'))
const Fzone = lazy(() => import('@views/my/fzone'))
const Favorite = lazy(() => import('@views/my/favorite'))
const Setting = lazy(() => import('@views/my/setting'))
const Error = lazy(() => import('@views/404'))

import Menu from '@/layouts/menu'
import Aside from '@/layouts/aside'

// 加载提示
const SpinLoading = () => {
    return (
        <div className="rcLoading">
            <Spin size="20" tip='loading...' />
        </div>
    )
}

// 延迟加载
const lazyload = children => {
    // React 16.6 新增了<Suspense>组件,让你可以“等待”目标代码加载,并且可以直接指定一个加载的界面,让它在用户等待的时候显示
    // 路由懒加载报错:react-dom.development.js:19055 Uncaught Error: A component suspended while responding to synchronous input.
    // 懒加载的模式需要我们给他加上一层 Loading的提示加载组件
    return <Suspense fallback={<SpinLoading />}>{children}</Suspense>
}

// 路由鉴权验证
const RouterAuth = ({ children }) => {
    const authState = authStore()

    return authState.isLogged ? (
        children
    ) : (
        <Navigate to="/login" replace={true} />
    )
}

export const routerConfig = [
    {
        path: '/',
        element: <RouterAuth><RouterLayout /></RouterAuth>,
        children: [
            // 首页
            { index: true, element: <Index /> },

            // 通讯录模块
            { path: '/contact', element: <Contact /> },
            { path: '/uinfo', element: <Uinfo /> },
            { path: '/newfriend', element: <NewFriend /> },

            // 聊天模块
            { path: '/chat', element: <Chat /> },
            { path: '/chatinfo', element: <ChatInfo /> },
            { path: '/redpacket', element: <RedPacket /> },

            // 我的模块
            { path: '/fzone', element: <Fzone /> },
            { path: '/favorite', element: <Favorite /> },
            { path: '/setting', element: <Setting /> },

            // 404模块 path="*"不能省略
            { path: '*', element: <Error /> }
        ]
    },
    // 登录/注册
    { path: '/login', element: <Login /> },
    { path: '/register', element: <Register /> }
]

const Router = () => useRoutes(routerConfig)

export default Router

整个项目布局分为侧边菜单/中间栏/右侧内容三大部分。

// 路由占位模板(类似vue中router-view)
const RouterLayout = () => {
    const authState = authStore()
    return (
        <div className="rc__container flexbox flex-alignc flex-justifyc" style={{'--themeSkin': authState.skin}}>
            <div className="rc__layout flexbox flex-col">
                {/* <div className="rc__layout-header">顶部栏</div> */}
                <div className="rc__layout-body flex1 flexbox">
                    {/* 菜单栏 */}
                    <Menu />

                    {/* 中间栏 */}
                    <Aside />

                    {/* 主内容区 */}
                    <div className="rc__layout-main flex1 flexbox flex-col">
                        { lazyload(<Outlet />) }
                    </div>
                </div>
            </div>
        </div>
    )
}

react18状态管理插件zustand

react18-webchat采用了全新的状态管理库Zustand4.x。替代了之前使用的redux状态管理。
zustand支持react18 hooks,易于上手,使用方便,内置了多种中间件。

// NPM
npm install zustand

// Yarn
yarn add zustand
/**
 * react18状态管理库Zustand
*/
import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'

export const authStore = create(
    persist(
        (set, get) => ({
            isLogged: false,
            token: null,
            // 折叠侧边栏
            collapse: false,
            // 个性换肤
            skin: null,
            // 登录数据
            loggedData: (data) => set({isLogged: data.isLogged, token: data.token}),
            setCollapse: (v) => set({collapse: v}),
            setSkin: (v) => set({skin: v})
        }),
        {
            name: 'authState',
            // name: 'auth-store', // name of the item in the storage (must be unique)
            // storage: createJSONStorage(() => sessionStorage), // by default, 'localStorage'
        }
    )
)

使用语法上比较类似vue3 pinia状态管理。

Ok,基于react18+acro开发网页聊天项目就先分享到这里了。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Web前端工程师
手记
粉丝
28
获赞与收藏
92

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消