react-select 默认值不适用于多个 selects

react-select default value not working for multiple selects

我正在使用 react-select 来显示服务列表,我想将前两个服务显示为默认服务,但 defaultValue 对我不起作用。
我正在使用 react-select 版本 4.3.1.


const [selectedservices, setselectedservices] = useState([]);
const services = [
{id: 70, value: 'today', label: 'today'},
{id: -1, value: 'All Services', label: 'All Services'}
{id: 81, value: 'plan', label: 'plan'},
{id: 79, value: 'time', label: 'time'},
{id: 78, value: 'recurring', label: 'recurring'},
{id: 76, value: 'jj (Copy)', label: 'jj (Copy)'},
{id: 75, value: 'final maybe', label: 'final maybe'}
];

 const handleService = (e) => {
    const a = [];
    e.map((i) => a.push(i.id));
    setselectedservices([...a]);
  };

<Select
   isMulti
   placeholder="All Services"
   onChange={(e) => handleService(e)}
   defaultValue={services.slice(0, 2).map((i) => i)}
   options={services}
   className="basic-multi-select"
   classNamePrefix="select"
/>

value 字段有效但 defaultValue 字段无效,如何设置 react-select 的默认值?

它应该运行良好,请检查错误或从其他地方设置

如果 services 来自 BE,您宁愿使用 value 而不是 defaultValue

这是根据您的代码生成的工作示例: