r/react • • 5d ago

Project / Code Review use-places-autocomplete fails on new Google Cloud projects, so I wrote a hook on Places API (New)

If you’ve set up Places autocomplete on a new Google Cloud project lately, you may have run into LegacyApiNotActivatedMapError. The legacy Places API, which AutocompleteService calls, is hidden from new projects and left out of new API keys, and Google says it can no longer be enabled for new projects. use-places-autocomplete and react-places-autocomplete both still depend on it.

So I wrote google-places-autocomplete-hook. It:

  • runs on AutocompleteSuggestion (Places API (New))
  • keeps one session token per search and ends it with the details call, so typing plus the pick is billed as one session, without you touching tokens
  • debounces, and drops responses that come back out of order
  • is headless: you render the input and the list
  • works with React 16.8 to 19 and server rendering, with no dependencies
  • has a /compat entry that returns use-places-autocomplete’s shape, so moving over is an import change

Demo: https://ninadsutrave.github.io/google-places-autocomplete-hook/

It runs against Google with your own key. Google’s free Maps Demo Key works and needs no billing; the page shows how to get one. It also logs every request the hook sends and which session it belongs to.

Repo: https://github.com/ninadsutrave/google-places-autocomplete-hook
npm: https://www.npmjs.com/package/google-places-autocomplete-hook

What’s missing for your use case?

2 Upvotes

0 comments sorted by