You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React JS中实现点击Logout按钮动态显示圆形加载效果

实现React中带加载Spinner的Logout/Login按钮切换

这是个很常见的交互场景,咱们可以通过React的状态管理轻松实现这个效果,核心思路就是用状态控制加载状态和按钮文本的切换,配合Font Awesome的旋转图标来展示加载动画。

第一步:准备Font Awesome

如果你还没在项目中引入Font Awesome,有两种常用方式:

  1. 通过npm安装(推荐):
    运行下面的命令安装依赖:
    npm install @fortawesome/react-fontawesome @fortawesome/free-solid-svg-icons
    
  2. 通过CDN引入:
    在你的index.html中添加CDN链接:
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    

第二步:编写组件代码

下面分别给出两种Font Awesome引入方式对应的组件实现:

方式一:使用npm安装的Font Awesome

import { useState } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCircleONotch } from '@fortawesome/free-solid-svg-icons';

function AuthButton() {
  // 管理加载状态和按钮显示文本
  const [isLoading, setIsLoading] = useState(false);
  const [buttonText, setButtonText] = useState('Logout');

  const handleAuthAction = () => {
    // 点击后立即进入加载状态
    setIsLoading(true);

    // 这里模拟登出的异步操作(实际项目中替换成你的API请求)
    setTimeout(() => {
      // 加载完成后切换文本为Login,并结束加载状态
      setButtonText('Login');
      setIsLoading(false);
    }, 2000); // 这里设置2秒的加载时长,可根据需求调整
  };

  return (
    <button 
      onClick={handleAuthAction}
      disabled={isLoading} // 加载时禁用按钮,防止重复点击
      style={{ 
        padding: '8px 16px', 
        cursor: isLoading ? 'not-allowed' : 'pointer',
        border: 'none',
        borderRadius: '4px',
        backgroundColor: '#dc3545',
        color: 'white'
      }}
    >
      {isLoading ? (
        // 加载状态下显示旋转的spinner图标
        <FontAwesomeIcon icon={faCircleONotch} spin />
      ) : (
        // 非加载状态显示按钮文本
        buttonText
      )}
    </button>
  );
}

export default AuthButton;

方式二:使用CDN引入的Font Awesome

import { useState } from 'react';

function AuthButton() {
  const [isLoading, setIsLoading] = useState(false);
  const [buttonText, setButtonText] = useState('Logout');

  const handleAuthAction = () => {
    setIsLoading(true);

    setTimeout(() => {
      setButtonText('Login');
      setIsLoading(false);
    }, 2000);
  };

  return (
    <button 
      onClick={handleAuthAction}
      disabled={isLoading}
      style={{ 
        padding: '8px 16px', 
        cursor: isLoading ? 'not-allowed' : 'pointer',
        border: 'none',
        borderRadius: '4px',
        backgroundColor: '#dc3545',
        color: 'white'
      }}
    >
      {isLoading ? (
        <i className="fa fa-circle-o-notch fa-spin"></i>
      ) : (
        buttonText
      )}
    </button>
  );
}

export default AuthButton;

关键细节说明

  • 状态管理:用useState维护isLoading(控制是否显示spinner)和buttonText(控制按钮文本)两个状态,这是React中处理动态UI的核心方式。
  • 防止重复操作:加载状态下禁用按钮,并把光标设为not-allowed,避免用户重复点击触发多次操作。
  • 异步模拟:示例中用setTimeout模拟登出的异步请求,实际项目中你需要把这部分替换成真实的后端API调用(比如fetch或者axios请求登出接口)。
  • 样式自定义:示例中给按钮加了基础样式,你可以根据自己项目的设计规范调整颜色、 padding、边框等样式。

内容的提问来源于stack exchange,提问作者shiva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:19:31