Why yours basic example doesn't work for me
See original GitHub issueHello,
I’m trying to use yours basic example to be able to understand how to use your module in my system but seems nothing to work. I don’t have any console errors and have no idea what wrong actually. I hope you can help me to understand what I’m missing.
Thank you and the code
import { useLoadScript } from '@react-google-maps/api';
import useOnclickOutside from 'react-cool-onclickoutside';
import usePlacesAutocomplete, {
getGeocode,
getLatLng,
} from 'use-places-autocomplete';
const libraries = ['places'];
export default function AutoCompletePlaces() {
console.log('IS IT RUNNING ????');
const API_KEY = process.env.GOOGLE_PLACES_API_KEY;
const { isLoaded, loadError } = useLoadScript({
googleMapsApiKey: API_KEY,
libraries,
});
console.log('LOADED ????', isLoaded, loadError);
const {
ready,
value,
suggestions: { status, data },
setValue,
clearSuggestions,
} = usePlacesAutocomplete({
// requestOptions: {
// /* Define search scope here */
// },
debounce: 300,
});
console.log(status, data);
const ref = useOnclickOutside(() => {
// When user clicks outside of the component, we can dismiss
// the searched suggestions by calling this method
clearSuggestions();
});
const handleInput = e => {
// Update the keyword of the input element
setValue(e.target.value);
};
const handleSelect = ({ description }) => () => {
console.log(description);
// When user selects a place, we can replace the keyword without request data from API
// by setting the second parameter to "false"
setValue(description, false);
clearSuggestions();
// Get latitude and longitude via utility functions
getGeocode({ address: description })
.then(results => getLatLng(results[0]))
.then(({ lat, lng }) => {
console.log('📍 Coordinates: ', { lat, lng });
})
.catch(error => {
console.log('😱 Error: ', error);
});
};
const renderSuggestions = () =>
data.map(suggestion => {
const {
place_id,
structured_formatting: { main_text, secondary_text },
} = suggestion;
return (
<li key={place_id} onClick={handleSelect(suggestion)}>
<strong>{main_text}</strong> <small>{secondary_text}</small>
</li>
);
});
return loadError || !isLoaded ? (
<div>FAIL</div>
) : (
<div ref={ref}>
<input
value={value}
onChange={handleInput}
disabled={!ready}
placeholder="Where are you going?"
/>
{/* We can use the "status" to decide whether we should display the dropdown or not */}
{status === 'OK' && <ul>{renderSuggestions()}</ul>}
</div>
);
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
7 Ways to Lead by Example (and Why It Matters) - BetterUp
1. Sharpen your leadership skills. One of the best ways to lead by example is to learn how to do it. This means...
Read more >How to Answer “Why Do You Want to Work Here?”
Like a similarly problematic interview question — “Tell me about yourself” — “Why do you want to work here?” requires you to focus...
Read more >6 Reasons Why Your VLOOKUP is Not Working - Ablebits
This article will look at the 6 most common reasons why your VLOOKUP is not working.
Read more >How to correct a #N/A error - Microsoft Support
Top solution. The most common cause of the #N/A error is with XLOOKUP, VLOOKUP, HLOOKUP, LOOKUP, or MATCH functions if a formula can't...
Read more >Beginner Tutorial - AutoHotkey
Learn how to download and install AutoHotkey, to create a script, to use hotkeys and hotstrings, to send keystrokes, to run programs, etc....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For every one has some similar issues as mine.
My problem was that, the API loader is handle by React and by the package @react-google-maps/api and the loading was not done before the autocomplete was executed. This was provoking issues.
The solution was to wrap the loading in a sub-component and let finish the loading before passing to the autocomplete part
Yes will do that but I’m not on the pc. Will copy paste what I did with an explanation later.