Ega Setya Putra
Ega Setya Putra

Reputation: 1695

How to change and get state from TouchableHighlight onPress?

I have a problem on how to get and set the state from function that I call on onPress. However I have no problem to get and set somewhere else. How can I change or get state from onPress?

Here is mycode:

Button:

<TouchableHighlight style={styles.submit_button} underlayColor='#FCD4A9' onPress={this.saveAbsenceData}>
  <Text style={{textAlign:'center',color:'white'}}>
   Submit
  </Text>
</TouchableHighlight>

saveAbsenceData function:

saveAbsenceData() {

  () => this.setState({
    visible: true
  })

  let formdata = new FormData();

  formdata.append("start_date", ()=> this.state.startDate);
}

with code above the state is not changing and this.state.startDate is empty

Upvotes: 1

Views: 219

Answers (1)

Pir Shukarullah Shah
Pir Shukarullah Shah

Reputation: 4252

this might be because you are not binding this on your method saveAbsenceData. add following code snippet in your constructor.

this.saveAbsenceData.bind(this)

or try following

onPress={()=> this.saveAbsenceData()}

Upvotes: 1

Related Questions