Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

reactjs - react-select & apollo client not loading options

I am trying to load options from graphql api. The output is good, we receive data and we parse it to new object containing { value, label } but i am unable to stop the loading and show options in the dropdown of react-select.

here is my code

import React from 'react'
import { useLazyQuery } from '@apollo/client'
import { GET_ECONT_CITIES_SEARCH_QUERY } from '../../graphql/queries'
import AsyncSelect from 'react-select/async'

const CityAutocompleteSelect = (props) => {
    const [findCities, { loading, error, data }] = useLazyQuery(GET_ECONT_CITIES_SEARCH_QUERY)

    const loadOptions = (inputValue) => {
        if(inputValue.trim().length > 2) {
            findCities({ variables: { limit: 5, filter: { countryCode: 'BGR', searchFilter: inputValue } } })

            if(data && data.econtCitiesSearch) {
                console.log(data.econtCitiesSearch.map(city => ({ value: city.id, label: `${city.name}, ${city.country.name} ${city.postCode}` })))
                const options = data.econtCitiesSearch.map(city => ({ value: city.id, label: `${city.name}, ${city.country.name} ${city.postCode}` }))
    
                return options
            }
    
            return []
        } 
    }

    return(
        <AsyncSelect
            isLoading={loading}
            placeholder={error ? 'моля, опитайте по-късно' : 'прим. София'}
            defaultOptions={[]}
            loadOptions={loadOptions}
            noOptionsMessage={() => 'няма намерени резултати'}
        />
    )
}

export default CityAutocompleteSelect
question from:https://stackoverflow.com/questions/65904420/react-select-apollo-client-not-loading-options

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...