繁体   English   中英

返回 Promise 的函数,这是 Promise 解决后要使用的一组选项

[英]Function that returns a promise, which is the set of options to be used once the promise resolves

在我的代码中 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)}
                    />

文档

loadOptions道具允许用户从回调中解析...

或从返回的承诺中解决......

……但不是两者兼而有之。 如果您正在使用返回承诺的async function ,请通过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;
//  ^^^^^^
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM