繁体   English   中英

反应选择异步不起作用

[英]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};
    });
});

根据本文档 ,我试图获取数据并将其设置为与<Select.Async ... /> loadOptions属性所需的格式匹配。 如前所述,我为els.data获得了Undefined 谁能告诉我我在做什么错?

res.json()是异步的。 它返回一个承诺,在未来这样处理then

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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