如演示中所述,我正在使用mui-places-autocomplete 。
import React from 'react'
import SomeCoolComponent from 'some-cool-component'
import MUIPlacesAutocomplete, { geocodeByPlaceID } from 'mui-places-autocomplete'
class Example extends React.Component {
constructor() {
super()
// Setup your state here...
this.state = { coordinates: null }
this.onSuggestionSelected = this.onSuggestionSelected.bind(this)
}
onSuggestionSelected(suggestion) {
geocodeByPlaceID(suggestion.place_id).then((results) => {
// Add your business logic here. In this case we simply set our state with the coordinates of
// the selected suggestion...
// Just use the first result in the list to get the geometry coordinates
const { geometry } = results[0]
const coordinates = {
lat: geometry.location.lat(),
lng: geometry.location.lng(),
}
this.setState({ coordinates })
}).catch((err) => {
// Handle any errors that occurred when we tried to get geospatial data for a selected
// suggestion...
})
}
render() {
// Your render logic here...
}
}
export default Example
这在用户选择建议时有效。但是当用户清除这个搜索框时,我们如何捕捉到这个事件呢?
在以前的类似库中,我正在使用类似的东西:handleChange
但现在/与此库一起无法使用。