justin
justin

Reputation: 159

react-native. Reference Error value is not defined

I'm having a problem setting a state in react-native. I can console.log the value just fine but when I call setState() I get Reference error 'targetSpreadsheet' is not defined.

This is in following function

getCategories = (file) => {
        console.log(this.state.targetSpreadsheet); // works fine
        this.setState({targetSpreadsheet: file});  // targetSpreadsheet is not defined.
}

And a picker that calls it

  <SimplePicker
    ref={'picker2'}
    options={this.state.spreadsheetNames}
    onSubmit={(option) => {
      for(var i = 0; i < this.state.spreadsheets.files.length; i++){
        if(this.state.spreadsheets.files[i].name === option){
          let file = this.state.spreadsheets.files[i];
          this.getCategories(file);
          break;
        }
      }
    }}
  />

EDIT

constructor

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

    this.getCategories = this.getCategories.bind(this);
  }

Upvotes: 2

Views: 3420

Answers (1)

Firdous nath
Firdous nath

Reputation: 1587

This show you want to access the spreadsheets object that has files array in it

this.state.spreadsheets.files[I]

But In your constructor you have initialised targetSpreadsheet as and string object, so you are getting error.

this.state = {
      targetSpreadsheet: ''
    } 

Solution : You need to make it as an object with files as an empty array.

    this.state = {
              targetSpreadsheet: {
                  files:[]
               }
   }

Upvotes: 2

Related Questions