React native JSON and State -


i new react native , programming in general. need here.

i have simple login screen user enters email , password. when user clicks on login button, call web service check number of matching records in db email/password combination. web service returns count in json format. getting blank count. doubting has setting state.

json web service: {"subscriber":[{"count":"0"}]}

here code:

  constructor(props) {     super(props)      this.state = {       email: '',       password: '',       ucount: '',       data: []     };   }    _onpressbutton() {      this.getdata();      console.log("email:", this.state.email);     console.log("passwd:", this.state.password);      console.log("data", this.state.data);     console.log("ucount", this.state.ucount);      if (this.state.ucount < 1) {         alert.alert('fail')     } else {         this.props.navigation.navigate('loginsuccess', { email: this.state.email, password: this.state.password})           }   }   getdata(){     var url="myurl.php?email=" + this.state.email + "&password=" + this.state.password;     console.log("url:", url);     return fetch(url)     .then((response) => response.json())     .then((responsejson) => {      this.setstate({       data: responsejson.subscriber,       ucount: responsejson.subscriber[0].count     })     })     .catch((error) => {       console.error(error);     });   } 

on button click calling _onpressbutton function call getdata function call web service , count json. have verified url construction correct , returns correct json. when print

console.log("data", this.state.data); console.log("ucount", this.state.ucount); 

after calling getdata function data array null , count blank initialized

data [] ucount 

this means below code not working.

.then((responsejson) => {      this.setstate({       data: responsejson.subscriber,       ucount: responsejson.subscriber[0].count     }) 

please help.

i have 1 solution, maybe can you, first change data, using return promise, must wait function firts before console.log

getdata(){   return new promise ((resolve, reject) => {     var url="myurl.php?email=" + this.state.email + "&password=" + this.state.password;     console.log("url:", url);     return fetch(url)     .then((response) => response.json())     .then((responsejson) => {       this.setstate({         data: responsejson.subscriber,         ucount: responsejson.subscriber[0].count       })       resolve(responsejson);     })     .catch((error) => {       reject(error);     });  }) } 

and can change _onpressbutton() 1 :

_onpressbutton() {      this.getdata()     .then((response) => {         console.log("email:", this.state.email);         console.log("passwd:", this.state.password);          console.log("data", this.state.data);         console.log("ucount", this.state.ucount);          if (this.state.ucount < 1) {             alert.alert('fail')         } else {             this.props.navigation.navigate('loginsuccess', { email: this.state.email, password: this.state.password})             }     }).catch((error) => {       console.log(error);     }); } 

hope can you, :)


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -