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;
相关推荐
清灵xmf11 小时前
CC Switch:解决 AI 编程工具配置
前端·人工智能·cc switch
IT_陈寒11 小时前
Redis缓存击穿把我坑惨了,原来这样解决才靠谱
前端·人工智能·后端
mfxcyh11 小时前
Vue3 右键菜单实现方案(基于 vue3-context-menu)
前端
treesforest11 小时前
从IP地址归属地查询到IP地理位置精准查询指南
服务器·前端·网络
棉猴11 小时前
python海龟绘图之画布与窗口
javascript·python·html·setup·turtle·海龟绘图·screensize
LF男男11 小时前
WindmillBullect.cs
前端
小白学大数据11 小时前
Python 爬虫爬取应用商店数据:请求构造与数据解析
前端·爬虫·python·数据分析
pkowner11 小时前
若依分页问题及解决方法
java·前端·算法
AI_paid_community11 小时前
25k Star 登顶 GitHub:这个专门吃 K 线图长大的 AI,让我意识到之前三年都在裸奔
javascript·claude
golang学习记11 小时前
Cursor官方团队的AI指南:Cursor Team Kit
前端·cursor