Isquare Isquare - 1 month ago 22
React JSX Question

React-native 0.35 setState issue

Unable to update the state from the database. console.log(db_name) returns the data. but unable to access it on render / state/ global.var.
How to make react to access local variables so that it can be used in the render jsx.

import React, { Component } from 'react';

import { AppRegistry, Text, View } from 'react-native';

import axios from 'axios';

class Easycall extends Component {

constructor(props) {

super(props);

this.state = {name:"name",};

}

gettt(){

axios.get('http://192.168.43.236/dbzx.php')

.then(function (response) {

let db_name = response.data.name ;

this.setState({name:db_name});

});

}


render() {

this.gettt();

return (<Text>{this.state.name}</Text>);

}


}


AppRegistry.registerComponent('Easycall', () => Easycall);

Answer

Try to change :

gettt(){

  axios.get('http://192.168.43.236/dbzx.php')

  .then(function (response) {

  let db_name = response.data.name ;

  this.setState = ({name:db_name});

});

} 

to:

gettt(){

  axios.get('http://192.168.43.236/dbzx.php')

  .then((response) => {

  let db_name = response.data.name ;

  this.setState = ({name:db_name});

});

}