react ant design select 远程搜索,调用接口 显示 加载中,接口调用完毕,要是没有数据,显示暂无数据

javascript 复制代码
import { useState } from 'react';
import { Select, Spin } from 'antd';

const { Option } = Select;

const RemoteSearchSelect = () => {
  const [loading, setLoading] = useState(false);
  const [options, setOptions] = useState([]);

  const fetchData = async (value) => {
    setLoading(true);

    // 模拟调用接口获取数据
    try {
      const response = await fetch(`https://api.example.com/search?q=${value}`);
      const data = await response.json();

      if (data.length > 0) {
        setOptions(data);
      } else {
        setOptions([]);
      }
    } catch (error) {
      console.error('Error fetching data:', error);
    }

    setLoading(false);
  };

  const handleSearch = (value) => {
    // 远程搜索数据
    fetchData(value);
  };

  return (
    <Select
      showSearch
      onSearch={handleSearch}
      placeholder="搜索"
      notFoundContent={loading ? <Spin size="small" /> : options.length === 0 ? "暂无数据" : null}
    >
      {options.map((option) => (
        <Option key={option.value} value={option.value}>
          {option.label}
        </Option>
      ))}
    </Select>
  );
};

export default RemoteSearchSelect;
相关推荐
huohaiyu3 分钟前
从URL到页面的完整解析流程
前端·网络·chrome·url
阿星AI工作室2 小时前
一个简单Demo彻底理解前后端怎么连的丨Figma + Supabase + Vercel
前端·人工智能
aircrushin2 小时前
一拍即传的平替,完全免费的实时照片墙!
前端
鹏北海4 小时前
JSBridge 原理详解
前端
孟健4 小时前
我的网站被黑了:一天灌入 227 万条垃圾数据,AI 写的代码差点让我社死
前端
anOnion4 小时前
构建无障碍组件之Checkbox pattern
前端·html·交互设计
IT枫斗者6 小时前
IntelliJ IDEA 2025.3史诗级更新:统一发行版+Spring Boot 4支持,这更新太香了!
java·开发语言·前端·javascript·spring boot·后端·intellij-idea
N***p3656 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
NGC_66117 小时前
二分查找算法
java·javascript·算法
享誉霸王7 小时前
15、告别混乱!Vue3复杂项目的规范搭建与基础库封装实战
前端·javascript·vue.js·前端框架·json·firefox·html5