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

fix for Maximum update depth exceeded Feature Request

Open pratteekshaurya1994 opened this issue 2 years ago • 5 comments

I was getting error "Maximum update depth exceeded".

So I checked you file GooglePlacesAutocomplete.js

In that I noticed in you useEffect:

useEffect(() => { // This will load the default value's search results after the view has // been rendered _handleChangeText(stateText); return () => { _abortRequests(); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [props.query]);

I think your props.query in your dependency array is causing onChangeText of textInputProps to continuously run making _handleChangeText change state continuously , I removed props.query and had an empty dependency array. then there was no issue. Also if you give stateText then also no issue comes.

please check it, and update it accordingly

pratteekshaurya1994 avatar Oct 23 '23 09:10 pratteekshaurya1994

I'm having the same issue. The onChangeText is called infinitively

nguyendinhdoan avatar Dec 11 '23 16:12 nguyendinhdoan

I've fixed this. So instead of using:

<GooglePlacesAutocomplete
  query={{
    key: 'YOUR API KEY',
    language: 'en'
  }}
/>

I change to:

const query = useRef({
  key: 'YOUR API KEY',
  language: 'en'
}).current;

<GooglePlacesAutocomplete
  query={query}
/>

nguyendinhdoan avatar Dec 11 '23 17:12 nguyendinhdoan

@nguyendinhdoan Life savior ! It fixes the issue :) What I did personally is that I totally removed the query variable from the component :

const query = {
  key: 'YOUR API KEY',
  language: 'en'
};

const MyComponent = (): JSX.Element => {
 return <GooglePlacesAutocomplete query={query} ... />;
}

yonitou avatar Dec 12 '23 12:12 yonitou

I've fixed this. So instead of using:

<GooglePlacesAutocomplete
  query={{
    key: 'YOUR API KEY',
    language: 'en'
  }}
/>

I change to:

const query = useRef({
  key: 'YOUR API KEY',
  language: 'en'
}).current;

<GooglePlacesAutocomplete
  query={query}
/>

@nguyendinhdoan Thanks! It works!

parnekov avatar Jul 31 '24 15:07 parnekov

@nguyendinhdoan Thank you so much ~

For those of you unlucky ones like me maintaining an ancient codebase, and after a routine SDK update, your client informs you next day that the app is crashing (what else is new?), you spend hours through thousands of lines of spaghetti code, only to discover—surprise!—it's GooglePlacesAutocomplete causing the havoc. Here is what you do for class component -

query = {
    key: '',
    language: 'en'
}

render() {
    return (
        ...
        <GooglePlacesAutocomplete
            ...
            query={this.query} 
        />
        ...
    )
}

successful-fella avatar Sep 14 '24 17:09 successful-fella