wellyshen / use-places-autocomplete

๐Ÿ˜Ž ๐Ÿ“ React hook for Google Maps Places Autocomplete.
https://use-places-autocomplete.netlify.app
MIT License
1.26k stars 65 forks source link

Cache key not updating on change #952

Closed sallomendo closed 2 years ago

sallomendo commented 2 years ago

Bug Report

Describe the Bug

fetchPredictions callback is not updating when cache or cacheKey changes.

How to Reproduce

When the cache key is stateful, the window storage key doesn't change.

const [countryCode, setCountryCode] = useState("US")

const autocomplete = usePlacesAutocomplete({
    cacheKey: countryCode
});

Expected Behavior

The cache and cache key should change when you pass a different value.

Your Environment

Additional Information

I think we need to add cache and cacheKey as a dependency to the useCallback hook here https://github.com/wellyshen/use-places-autocomplete/blob/master/src/usePlacesAutocomplete.ts#L155

wellyshen commented 2 years ago

@sallomendo Ask a DX question. Should we need to remove the previously cached data when the key is updated?

sallomendo commented 2 years ago

@wellyshen I donโ€™t think so, we can use the clearCache function inside a useEffect hook, I think we should leave that to the dev.

wellyshen commented 2 years ago

@sallomendo Sorry quite busy recently, would you mind to give me a PR for this issue?

wellyshen commented 2 years ago

@sallomendo This issue has been fixed in the latest version.

sallomendo commented 2 years ago

@wellyshen thanks for working on this, I will close the pr I created for this.

wellyshen commented 2 years ago

@sallomendo Sorry I didn't note that, thank you!