0
我使用的是React Select,我想將url
作爲參數傳遞給loadOptions
。所以,我想是這樣的:將道具傳遞給道具
<Select.Async {...input} loadOptions={ getOptions(url) }/>
,這是我getOptions
:
const getOptions = ((input, callback) => {
return fetch("http://reqres.in/api/users") // Get my url HERE
.then((res) => {
return res.json();
}).then((json) => {
return {options: json.data};
});
});
反正有沒有做到這一點?提前致謝。
SOLUTION:
最簡單的解決辦法是這樣的:
<Select.Async {...input} loadOptions={ getOptions.bind(url) }/>
和getOptions
希望這有助於把this
而不是URL的;)
看到獲取選項是異步的,您可能必須將URL設置爲狀態,然後在getOptions()返回時更新它 – Jayce444