简体   繁体   中英

react-select async doesn't work

const formatData = ((els) => {
  console.log("ELS : ", els.data); /* Got Undefined */
  return _.each(els, (el) => ({
    label: el.first_name,
    value: el.id,
  }));
});


const fetchOptions = ((input, callback) => {
  return fetch("http://reqres.in/api/users")
    .then((res) => {
      callback(null,
        {
          options: formatData(res.json())
        }
      )
    }).then((json) => {
      return {options: json};
    });
});

Based to this documentation , I'm trying to get data and set it to match the format required by loadOptions property of <Select.Async ... /> . As I mentioned above, I got Undefined for els.data. Can anyone tell me what I'm doing wrong ?

res.json() is asynchronous. It returns a Promise, so handle in the next then .

const fetchOptions = ((input, callback) => {
  return fetch("http://reqres.in/api/users")
    .then((res) => {
      return res.json();
    }).then((json) => {
      // formatData(json);
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM