2017-07-17 81 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