0

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

5
  • I imagine the response object doesn't look like what you think it looks like. Maybe try logging the response in getReviewApp Commented Oct 23, 2019 at 15:22
  • Yes does because it set the correct values reason = Test and selectedApprove = Yes, but for some reason the state is reloaded with null values Commented Oct 23, 2019 at 15:40
  • I can't see anything weird. Maybe try what @tom suggested Commented Oct 23, 2019 at 18:35
  • Either that or there is more code not mentioned further modifying state. Commented Oct 23, 2019 at 19:43
  • 1
    @tom thanks. The server was off Commented Oct 23, 2019 at 20:02

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.