I have a Google Map and I am trying to update the map center with a Google Autocomplete search. I have the Searchbar in a separate JSX file from the recenter map function. How can I call this function from within my searchbar file? Below is what I tried, with this I get the error TypeError: __WEBPACK_IMPORTED_MODULE_1__map__.a.recenterMap is not a function
on the line with the comment above it
Here is my working example, and here is my code(updated based off AlainIb's response):
map.js:
export class MapContainer extends React.Component {
...
recenterMap(lat, lng) {
const map = this.map;
const google = this.props.google;
const maps = google.maps;
if (map) {
let center = new maps.LatLng(lat, lng);
map.panTo(center);
}
}
...
}
export default MapContainer;
And here is my searchbar.jsx file:
import MapContainer from './map';
/* global google */
class SearchBar extends Component {
...
handlePlaceChanged(){
const place = this.autocomplete.getPlace();
this.setState({ lat: place.geometry.location.lat() });
this.setState({ lng: place.geometry.location.lng() });
/* WHAT I TRIED */
MapContainer.recenterMap(this.state.lat, this.state.lng);
}
...
}
export default SearchBar;