myTest532 myTest532
myTest532 myTest532

Reputation: 2381

React Native state being changed after set values

I have a very simple component which set state values using axios. However, the state value is being changed in the render method.

constructor(props) {
    super(props);
    const { navigation } = this.props;
    const approveID = navigation.getParam('approveID', '0');
    this.state = {
      selectedApprove: approveID,
      reason: '',
    };
}

componentDidMount() {
    const { navigation } = this.props;
    const tenantID = navigation.getParam('tenantID', '0');
    this.getReviewApp(tenantID);
}

getReviewApp(tID) {
    axios.get('http://myserver/getData', {
      params: {
        method: 'getApplicantReview',
        tenantID: tID
      }
    }).then((response) => {
      const result = response.data.DATA[0];
      this.setState({
        selectedApprove: result[0],
        reason: result[1]
      });
    }).catch((error) => {
      // handle error
      console.log(error);
    });
}

...

render() {
   console.log(this.state);
...
}

When I run the app the console shows 2 times. First is perfect: Object { "reason": "Test", "selectedApprove": "Yes", }

The second log is with null values and it messes up my component: Object { "reason": null, "selectedApprove": null, }

Why is it happening?

Thanks

Upvotes: 0

Views: 41

Answers (1)

tom
tom

Reputation: 2365

The response object must not look like what you think it looks like.

Upvotes: 1

Related Questions