Möchten Sie Optionen für die Änderung des Eingabewerts in der Reaktionsauswahl erhalten?
P粉775723722
P粉775723722 2023-09-14 13:14:36
0
2
390

Ich habe eine React-Select-Komponente und möchte eine Funktionalität hinzufügen, sodass, sobald jemand etwas in React-Select eingibt, eine API-Anfrage erfolgen sollte, um die Elemente abzurufen, die sich auf das eingegebene Schlüsselwort beziehen. Wie kann ich das tun

P粉775723722
P粉775723722

Antworte allen(2)
P粉354602955

你可以做到这一点,无需任何 API 调用,只需使用过滤器方法来过滤你的选项

P粉675258598

您应该尝试从“react-select/async”查看 AsyncSelect 然后在组件中创建一个函数来从 API 加载选项,该函数应接受输入字符串和回调,并应根据输入字符串进行 API 调用。像这样的事情

const loadOptions = (inputValue, callback) => {
    // api call here
    fetch('your-api-url?${inputValue}')
      .then(response => response.json())
      .then(data => {
         // do your work here
         const options = //transform data here
         callback(options)
      });
};

然后在您的组件中将 loadOptions 函数传递到 loadOptions 属性中

const YourComponent = () => {
    return (
       <AsyncSelect
         cacheOptions
         defaultOptions
         loadOptions={loadOptions}
       />
    );
};
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!