user11331566
user11331566

Reputation:

Ensuring a function runs after setState

I know that SetState takes a callback, which I´m using. For some reason, though, it is not running after the state updates. Am I doing something wrong?

  handleInputChange = (event) => {
        this.setState({
          [name]: event.target.value
        });
          if(event.target.value.length > 1) {
          this.getSuggestions();
          this.setState({
          query: event.target.value,
        }, () => this.getCoordinates)
      }

getCoordinates = () =>{
    const geocoder = new google.maps.Geocoder();
    geocoder.geocode( {"address":this.state.query}, this.onSuccessGetAddress)
  }

Upvotes: 1

Views: 48

Answers (1)

Dylan Walker
Dylan Walker

Reputation: 1127

You provided a callback which returns the function this.getCoordinates:

() => this.getCoordinates

You want to provide a callback which calls this.getCoordinates:

() => this.getCoordinates()

or

this.getCoordinates

Upvotes: 5

Related Questions