Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react function is not defined no-undef

Tags:

reactjs

I get the following error when trying to compile my app - 'getRestaurants' is not defined no-undef

I'm having trouble tracking down why getRestaurants is not defined for zipCodeToCoords

I have an input component that takes a zip code on submit and calls zipCodeToCoords, I then want to call getRestaurants

Here is the main react component

class App extends Component {
  constructor(props){
    super(props);
    this.state = {
      restraunts:[],
      selectedRestraunt:null
    }
  }


  getRestaurants(lat,lon){
    var url = "https://developers.zomato.com/api/v2.1/geocode?lat="+lat+"&lon="+lon;
    $.ajax({
      url:url,
      headers:{
        'Accept': 'application/json',
        'user_key': 'MY KEY'
      }

    }).done(function(data){
      console.log(data);
    });
  }


  zipCodeToCoords(zip){
    var url = "https://www.zipcodeapi.com/rest/MYKEY/info.json/"+ zip +"/degrees";
    $.ajax({
      url:url,

    }).done(function(data){
      console.log(data.lat);
      getRestaurants(data.lat,data.lng);
    });
  }




  render() {
    return (
      <div className="App">
        <div className="row">
          <div className="columns large-9">
            <Input onSearchTermSubmit={this.zipCodeToCoords} />
          </div>  
        </div>
        <div className="row">
          <div className="columns large-9">
            <RestrauntInfo />
          </div>
          <div className="columns large-3">
            <RestrauntList />
          </div>
        </div>
      </div>
    );
  }
}

Here is the Input component

class Input extends Component{

    constructor(props){
        super(props);
        this.state = {zip:''};

        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }


    handleSubmit(event){
        event.preventDefault();
        this.props.onSearchTermSubmit(this.state.zip);
    }
    handleChange(event){
        this.setState({zip:event.target.value});

    }

    render(){
        return(<div>
            <form onSubmit={this.handleSubmit}>
                <div className="input-group">
                    <span className="input-group-label">Enter Zip Code</span>
                    <input className="input-group-field" type="number" value={this.state.zip} onChange={this.handleChange} />
                    <div className="input-group-button">
                      <input type="submit" className="button" value="Submit" />
                    </div>
                </div>  
            </form> 
        </div>);
    }
}
like image 731
Cody Krauskopf Avatar asked Jun 11 '17 17:06

Cody Krauskopf


People also ask

Can we use PyScript in React?

Yes, as you said, PyScript comes with CSS and I sometimes feel like customizing a cell. Pyodide has good documentation on available APIs.

Can I use Streamlit in React?

One of the main benefits of Streamlit is that it enables you to assign each call to an independent React component with its own state and logic, all rendered in your web browser. We could actually say Streamlit is a bridge between React and Python.

How do you fix useState is not defined?

The "Uncaught ReferenceError: useState is not defined" occurs when we use the useState hook in our code but forget to import it. To solve the error, import the hook before using it - import {useState} from 'react' . Copied!


1 Answers

Get restaurants is not some global function, it is a method on your App class so you need to call it as such. The easiest way to resolve this would probably be to:

  1. Change your done callback to an arrow function
  2. Call this.getRestaurants in zipCodeToCoords
  3. Bind this to getRestaurants and zipCodeToCoords in your constructor i.e. this.getRestaurants = this.getRestaurants.bind(this);

The callback would become:

done((data) => {
    console.log(data.lat);
    this.getRestaurants(data.lat,data.lng);
});
like image 103
ExoticChimp Avatar answered Oct 13 '22 06:10

ExoticChimp