localvore-today / react-mapbox-autocomplete

MIT License
15 stars 17 forks source link

Unable to set State #12

Open tbiinfotech opened 6 years ago

tbiinfotech commented 6 years ago
<MapboxAutocomplete publicKey='custom_key'
                    inputClass='form-control search btn dropdown-toggle'
                    onSuggestionSelect={this._suggestionSelect}
                    country=''
                    resetSearch={false}
                    placeholder="Enter Location"
                    />

On Select the auto complete result in list, function is getting called

_suggestionSelect(result, lat, lng, text) {
    console.log(result, lat, lng, text)
    this.setState({center: [lat,lng]})
}

But here I am getting following error:

Uncaught TypeError: this.setState is not a function

bakedbean commented 6 years ago

@tbiinfotech you have to bind the method to the parent scope: onSuggestionSelect={this._suggestionSelect.bind(this)}

sfsr commented 5 years ago

@tbiinfotech you can use arrow function:

_suggestionSelect = (result, lat, lng, text) => {
    console.log(result, lat, lng, text)
    this.setState({center: [lat,lng]})
}