Want to get options on inputValue change in react select
P粉775723722
P粉775723722 2023-09-14 13:14:36
0
2
435

I have a react-select component and I want to add a functionality so that once someone enters something in react-select there should be an api request to get the items related to the entered keyword, how can I this way

P粉775723722
P粉775723722

reply all(2)
P粉354602955

You can do this without any API calls, just use the filter method to filter your options

P粉675258598

You should try to view AsyncSelect from "react-select/async" Then create a function in the component to load the options from the API, the function should accept an input string and a callback and should make an API call based on the input string. Things like this

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)
      });
};

Then pass the loadOptions function into the loadOptions property in your component

const YourComponent = () => {
    return (
       <AsyncSelect
         cacheOptions
         defaultOptions
         loadOptions={loadOptions}
       />
    );
};
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template