2017-07-17 61 views
0

鉴于在react-select(https://jedwatson.github.io/react-select/)中做出的选择,我使用axios(https://github.com/mzabriskie/axios)从远程URL获取数据以填充页面上的第二个react-select。异步填充react-select

我认为我的用例与Async示例不同的技巧是我不希望用户必须完全键入第二个选择才能触发自动填充。我想要在AJAX调用返回数据时立即发生自动填充

我确认我正确抓取远程数据,但我无法完全弄清楚loadOptions参数的正确语法反应选择。在代码中我想要的最接近的例子是在这个函数从该项目的例子

https://github.com/JedWatson/react-select/blob/master/examples/src/components/GithubUsers.js#L36

感谢任何提示,你可以提供

回答

0

在github上异步例如,输入参数getUsers(input)是搜索文本。

您可以在第一反应选值存储在组件状态,比loadOptions承诺将使用这个状态值firstSelectValue

getUsers() { 
    return fetch(`https://api.github.com/search/users?q=${this.state.firstSelectValue}`) 
    .then((response) => response.json()) 
    .then((json) => { 
     return { options: json.items }; 
    }); 
} 
+0

你为什么存储状态的第一个值取终点? – alexb

+0

https://reactjs.org/docs/state-and-lifecycle.html – stoufa88