leiwuhen-67's blog leiwuhen-67's blog
首页
    • 《Vue》笔记
    • 《React》笔记
    • 《NodeJs》笔记
    • 《CSS》笔记
    • 《Redis》笔记
    • 基础入门
    • 《Mock》笔记
    • 《MySQL》笔记
    • 《Git》相关
影音视听
收藏
关于
GitHub (opens new window)

我的公众号

首页
    • 《Vue》笔记
    • 《React》笔记
    • 《NodeJs》笔记
    • 《CSS》笔记
    • 《Redis》笔记
    • 基础入门
    • 《Mock》笔记
    • 《MySQL》笔记
    • 《Git》相关
影音视听
收藏
关于
GitHub (opens new window)
  • React

    • 使用React脚手架快速搭建项目
    • hooks之useEffect
    • React之事件绑定及简写方式
    • React之props传值并对其进行限制
    • React之Refs的基本使用
    • React之生命周期钩子
    • React之key的使用
    • React之代理服务器配置
    • React之封装全局Loading组件
      • 1、安装antd-mobile、axios
      • 2、Loading组件显示与隐藏方法封装,建立Loading.js文件
      • 3、axios简单封装
    • React之消息的发布-订阅(pubsub-js)
    • React之Redux的基本使用
    • React之react-redux的基本使用
    • React之redux的数据持久化存储
    • React之路由懒加载
    • React之Render Props的使用
    • React之createBrowserRouter
    • React之路径别名@配置
    • React之craco打包优化配置
    • React之项目国际化
    • React之postcss-pxtorem移动端适配
    • React之使用vite创建项目
    • React之ts类型标注汇总
    • React之使用 ant design搭建后台管理之踩坑
    • React之路由切换动画
    • React之使用vite创建组件库并发布到npm
    • React之项目打包部署到nginx
    • React之自定义组件添加className与style
  • React Native

  • 《React》笔记
  • React
心欲无痕
2024-03-14
目录

React之封装全局Loading组件

在日常项目开发中,由于经常需要发送网络请求,在这期间,页面可能会出现短暂的白屏,因此需要有一个 loading 效果以提升用户体验。

这里我使用了 antd-mobile 5.2.2 中的 SpinLoading 转圈组件,使用了 axios 的请求拦截器和相应拦截器来控制 loading 的展示与隐藏。

# 1、安装 antd-mobile、axios

npm install antd-mobile axios
1

# 2、Loading 组件显示与隐藏方法封装,建立 Loading.js 文件

import React from "react";
import ReactDOM from "react-dom";
import { SpinLoading } from "antd-mobile";
import "./Loading.css";

// 当前正在请求的数量
let requestCount = 0
function LoadingComponent (props) {
    return (
        <div className="loading-box" style={{backgroundColor: props.showBg ? "rgba(0, 0, 0, 0.7)" : "transparent"}}>
            <div className="loading-wrapper">
                <SpinLoading color="primary" />
                <p className="loading-text">{ props.loadingText }</p>
            </div>
        </div>
    )
}

const Loading = {
    show(config = {loadingText: "玩命加载中...", showBg: false}) {
        if (requestCount === 0) {
            const dom = document.createElement('div');
            dom.setAttribute('id', 'loading')
            document.body.appendChild(dom)
            ReactDOM.createRoot(dom).render(<LoadingComponent {...config} />);
        }
        requestCount++
    },
    hide() {
        if (requestCount > 0) {
          requestCount--;
        }
        if (requestCount === 0) {
            document.body.removeChild(document.getElementById('loading'))
        }
    },
}

export default Loading;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

Loading.css 代码如下:

#loading {
    width:100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
}
.loading-box {
    width:100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    display: flex;
    justify-content: center;
    align-items: center;
}
.loading-wrapper {
    padding: 20px 40px;
    border-radius: 10px;
    background: #fff;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
.loading-wrapper .loading-text {
    padding: 0;
    margin: 10px 0 0;
    font-size: 18px;
    color: #000000;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 3、axios 简单封装

在项目 src 文件夹下建立 networking 文件夹,在该文件夹下建立 index.js 文件,代码如下

import axios from "axios"
import Loading from "../components/Loading/Loading";  // 引入Loading文件

axios.defaults.baseURL = "http://localhost:3002"

axios.interceptors.request.use((config) => {
  Loading.show()
  // 可在此处添加一些通用参数
  return config
}, (error) => {
  setTimeout(() => {
    Loading.hide()
  }, 200)
  return Promise.reject(error)
})

axios.interceptors.response.use((response) => {
  setTimeout(() => {
    Loading.hide()
  }, 200)
  return response
}, (error) => {
  setTimeout(() => {
    Loading.hide()
  }, 200)
  return Promise.reject(error)
})

export default axios
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

在项目 src 文件夹下的 index.js 文件中引入刚封装的 axios 请求方法

import "./networking"
1

至此,全局 Loading 组件就可以正常工作了。

编辑 (opens new window)
上次更新: 3/4/2025, 5:46:30 PM
React之代理服务器配置
React之消息的发布-订阅(pubsub-js)

← React之代理服务器配置 React之消息的发布-订阅(pubsub-js)→

Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式