未选择反应选择异步回调

问题描述 投票:0回答:1

我正在使用react-select的AsyncSelect组件,并尝试使用以下代码进行resolve it from a callback

loadOptions(inputValue, callback) {
  this.props.asyncFunctionWithCallback(resp => {
    callback(resp);
  });
}

asyncFunctionWithCallback()是一个异步函数,它接收回调在兑现承诺后即被调用:

asyncFunctionWithCallback(doneCallback) {
  // Call some async code
  fetch(url).then(response => { 
    doneCallback(response) 
  }
}

[我正在尝试从callback()的回调中调用react-select的asyncFunctionWithCallback(),但似乎没有被调用,因为asyncFunctionWithCallback()被永远重复调用。

我想我没有正确传递回调,但是无法弄清楚我在做什么错。

javascript reactjs react-select loadoptions
1个回答
0
投票
您需要将res.json值从获取传递给回调,例如

asyncFunctionWithCallback(doneCallback) { // Call some async code fetch(url)..then(res => res.json())then(response => { doneCallback(response) } }

但是,由于您已经有了异步代码,因此最好对负载选项使用promist方法

loadOptions(inputValue) { return this.props.asyncFunctionWithCallback(); } asyncFunctionWithCallback() { // Call some async code return fetch(url)..then(res => res.json()); }

© www.soinside.com 2019 - 2024. All rights reserved.