在我的代碼中 loadOptions 給了我以下錯誤訊息“回傳承諾的函式,這是承諾解決后要使用的選項集。” 但我已經在代碼中做了一些嘗試,但沒有成功。誰能幫我理解 loadOptions 中出現的這個錯誤?
import { useState } from 'react';
import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet';
import AsyncSelect from 'react-select/async';
import { fetchLocalMapBox } from '../api';
const initialPosition = {
lat: -22.7532328,
lng: -43.4563604
}
type Place = {
label?: string;
value?: string;
position: {
lat: number;
lng: number;
};
}
function OrderLocation() {
const [address, setAddress] = useState<Place>({
position: initialPosition
})
const loadOptions = async (inputValue: string, callback: (places: Place[]) => void) => {
const response = await fetchLocalMapBox(inputValue);
const places = response.data.features.map((item: any) => {
return ({
label: item.place_name,
value: item.place_name,
position: {
lat: item.center[1],
lng: item.center[0]
},
place: item.place_name,
});
});
callback(places);
};
<div className="filter-container">
<AsyncSelect
placeholder="Digite um endere?o para entregar o pedido"
className="filter"
loadOptions={loadOptions}
onChange={value => handleChangeSelect (value as Place)}
/>
uj5u.com熱心網友回復:
檔案說
該
loadOptions道具允許用戶從回呼中解決...或從回傳的承諾中解決......
……但不是兩者兼而有之。如果您使用的async function是回傳承諾的 an,請通過設定值來使用選項解決承諾return。不接受回呼:
const loadOptions = async (inputValue: string) => {
const response = await fetchLocalMapBox(inputValue);
const places = response.data.features.map((item: any) => ({
label: item.place_name,
value: item.place_name,
position: {
lat: item.center[1],
lng: item.center[0]
},
place: item.place_name,
}));
return places;
// ^^^^^^
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/484098.html
標籤:javascript 打字稿 承诺 反应选择 加载选项
