I am trying to build a form, where you can pick a place at the end of the form. I am using places-autocomplete for this. The Locationpicker is a child of the Form-View Component. The state for the coordinates is set when I choose the place in handleSelect(), but if I want to add it to the form data in the parent, I can't access it.
Child component
class LocationPicker extends React.Component {
constructor(props) {
super(props);
this.state = {
address: "San Francisco, CA",
lat: "",
lng: ""
};
this.handleSelect = this.handleSelect.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleSelect(address) {
this.setState({
address
});
geocodeByAddress(this.state.address)
.then(results => getLatLng(results[0]))
.then(latLng => this.setState({ lat: latLng.lat, lng: latLng.lng }))
.catch(error => console.error("Error", error));
}
handleChange(address) {
this.setState({
address,
geocodeResults: null
});
}
render() {
const inputProps = {
value: this.state.address,
type: "text",
onChange: this.handleChange,
};
return (
<div>
<PlacesAutocomplete
onSelect={this.handleSelect}
onEnterKeyDown={this.handleSelect}
inputProps={inputProps}
/>
<p>{this.state.lat}</p>
<p>{this.state.lng}</p>
</div>
);
}
}
Now I would like to use lat and lng in my parent component, but I don't want to add the handleSelect function to my parent. How can I send lat and lng to my parent component?