react-places-autocomplete icon indicating copy to clipboard operation
react-places-autocomplete copied to clipboard

When start writing in the input just appear "loading" instead of the places drowpdown

Open mariaMPSerra opened this issue 5 years ago • 6 comments

Hi, I'm using react-places-autocomplete and when I start typing in the input to search for a place the only thing that appears is "loading..." instead of the dropdown with the suggestions. This is my code:

export default class LocationSearchInput extends React.Component {
  constructor(props) {
    super(props);
    this.state = { address: '',googleMapsLoading: false };
  }

  componentDidMount(){

    window.initMap = this.initMap
    const gmapScriptEl = document.createElement(`script`)
    gmapScriptEl.src = `https://maps.googleapis.com/maps/api/js?key=MY_KEY&libraries=places&callback=initMap`
    document.querySelector(`body`).insertAdjacentElement(`beforeend`, gmapScriptEl)
  }

  initMap = () => {
    this.setState({
      googleMapsLoading: true,
    })
  }

  handleChange = address => {
    this.setState({ address });
  };

  handleSelect = address => {
    geocodeByAddress(address)
      .then(results => getLatLng(results[0]))
      .then(latLng => console.log('Success', latLng))
      .catch(error => console.error('Error', error));
  };

  render() {
    return (
        <div>
        {

            this.state.googleMapsLoading ?
            <PlacesAutocomplete
        value={this.state.address}
        onChange={this.handleChange}
        onSelect={this.handleSelect}
      >
        {({ getInputProps, suggestions, getSuggestionItemProps, loading }) => (
          <div>
            <input
              {...getInputProps({
                placeholder: 'Search Places ...',
                className: 'location-search-input',
              })}
            />
            <div className="autocomplete-dropdown-container">
              {loading && <div>Loading...</div>}
              {suggestions.map(suggestion => {
                const className = suggestion.active
                  ? 'suggestion-item--active'
                  : 'suggestion-item';
                // inline style for demonstration purpose
                const style = suggestion.active
                  ? { backgroundColor: '#fafafa', cursor: 'pointer' }
                  : { backgroundColor: '#ffffff', cursor: 'pointer' };
                return (
                  <div
                    {...getSuggestionItemProps(suggestion, {
                      className,
                      style,
                    })}
                  >
                    <span>{suggestion.description}</span>
                  </div>
                );
              })}
            </div>
          </div>
        )}
      </PlacesAutocomplete> : null
        }
      </div>
    );
  }
}

I had to add the initMap function because I had the loader error. Thanks

mariaMPSerra avatar Aug 24 '20 12:08 mariaMPSerra

I have the same issue!

pgiouroukis avatar Aug 27 '20 08:08 pgiouroukis

Same too, sometimes i need to refresh it multiple times to work. Any workarounds?

codepurse avatar Sep 05 '20 12:09 codepurse

Encountered the same issue, any workaround?

Huzaifaahmed20 avatar Oct 07 '20 09:10 Huzaifaahmed20

Same issue. It is intermittent, most of the time it works fine but sometimes it just sits in "loading" forever.

Shaninnik avatar Oct 19 '20 15:10 Shaninnik

Are you using google maps also in your app?

Same issue. It is intermittent, most of the time it works fine but sometimes it just sits in "loading" forever.

Huzaifaahmed20 avatar Oct 20 '20 08:10 Huzaifaahmed20

Previously I was using google-map-react and this autocomplete library, which is making this loading problem, I think this library is not compatible with google-map-react, try using this react-google-maps, it will solve this loading issue and render places correctly

Huzaifaahmed20 avatar Oct 20 '20 08:10 Huzaifaahmed20