-
Notifications
You must be signed in to change notification settings - Fork 326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to get property 'getPlacePredictions' of undefined or null reference #130
Comments
I suppose your PR was a solution to this issue already? |
No, #128 was not a fix for this issue. This bug is unrelated. |
Do you have some test case? I cannot replicate this… |
It's been difficult to reproduce. I am looping in my colleague @olegpesok because I know he has been working on it |
@ro-ka Having trouble reproducing with react-test-utils. But I can with the example project in your source:
Full example:
The onInputFocus function fires BEFORE componentDidMount is executed. This causes the "Cannot read property 'getPlacePredictions' of undefined" error. |
Just released in version |
Since the
:) |
Referencing #172 as this is also connected to server-side rendering. Works fine without. |
@dragma how can the component render on the client when server-side rendering fails? |
I managed to get this error by doing this trick : if (typeof window !== 'undefined') {
return <Geosuggest
className={classes}
inputClassName="form-control"
country="fr"
fixtures={fixtures}
types={['(cities)']}
onSuggestSelect={suggest => console.log('onSuggestSelect', suggest)}
autoActivateFirstSuggest
// onSuggestNoResults
// onChange
/>;
} A bad idea at least. |
I've been trying to reproduce this. For all I can tell, the issue discussed here is a result of the workaround you posted above. |
I ran into this error today, and what fixed it for me was making sure |
@richcsmith im sorry can i have a code example? cant figure out how to hook up the im facing this issue as well |
@joevo2 here's a quick and dirty example based on some old code I wrote to solve this: /* utils/window.js */
export function loadScriptAsync(src) {
const script = window.document.createElement('script')
script.src = src
script.async = true
script.defer = true
const promise = new Promise((resolve, reject) => {
script.addEventListener('load', (event) => {
resolve(event)
}, false)
script.addEventListener('error', (error) => reject(error))
})
window.document.body.appendChild(script)
return promise
} /* lib/google.js */
import { loadScriptAsync } from 'utils/window'
const googleAPIKeys = {
development: GOOGLE_MAPS_DEV_KEY,
staging: GOOGLE_MAPS_STAGING_KEY,
production: GOOGLE_MAPS_PROD_KEY,
}
const getGoogleMapsAPIUrl = key => `https://maps.googleapis.com/maps/api/js?key=${key}&libraries=places`
export const initGoogleMapsAPI = () => loadScriptAsync(getGoogleMapsAPIUrl(googleAPIKeys[env])) /* components/Map/Map.jsx */
import GoogleMap from 'google-map-react'
import Icon from 'components/Icon/Icon'
import { initGoogleMapsAPI } from 'lib/google'
export default class Map extends React.Component {
state = {
gMapsLoaded: false,
}
componentDidMount = () => {
if (!window.google) {
initGoogleMapsAPI().then(() => {
this.setState({ gMapsLoaded: true })
})
}
}
render = () => {
if (!this.state.gMapsLoaded) {
return null
}
const { lat, lng } = this.props.address
const coordinates = {
lat: parseFloat(lat),
lng: parseFloat(lng),
}
return (
<GoogleMap center={coordinates} zoom={14}>
<Icon name="location-pin" {...coordinates} />
</GoogleMap>
)
}
} Edit: It's been a while since I last used this library—I just realized that this issue is for For this, I created an |
@richcsmith thanks! thats interesting, basically creating the script on react side hmm... Would something like |
@joevo2 I think it would! In my project, I had several components that each depended on the Google Maps API in different areas on the app (sometimes, on the same page), so I had to check for its existence before attempting to load it. |
I am seeing this bug periodically with this component. We are using it in production.
The google maps object must have been loaded up because I see that you throw an error in the componentDidMount function if it is not in the window.
Is it possible that the
this
keyword points to a reference other than the Geosuggest object?The bug has occurred in the following browsers:
Below is the stack trace:
The text was updated successfully, but these errors were encountered: