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;
相关推荐
niucloud-admin20 小时前
web 端前端
前端
摘星编程1 天前
React Native for OpenHarmony 实战:Linking 链接处理详解
javascript·react native·react.js
摘星编程1 天前
React Native for OpenHarmony 实战:DatePickerAndroid 日期选择器详解
android·react native·react.js
胖者是谁1 天前
EasyPlayerPro的使用方法
前端·javascript·css
EndingCoder1 天前
索引类型和 keyof 操作符
linux·运维·前端·javascript·ubuntu·typescript
liux35281 天前
Web集群管理实战指南:从架构到运维
运维·前端·架构
沛沛老爹1 天前
Web转AI架构篇 Agent Skills vs MCP:工具箱与标准接口的本质区别
java·开发语言·前端·人工智能·架构·企业开发
摘星编程1 天前
React Native for OpenHarmony 实战:ImageBackground 背景图片详解
javascript·react native·react.js
小光学长1 天前
基于Web的长江游轮公共服务系统j225o57w(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
前端·数据库
摘星编程1 天前
React Native for OpenHarmony 实战:Alert 警告提示详解
javascript·react native·react.js