# React state confusion

**URL:** <https://forum.freecodecamp.org/t/react-state-confusion/376403>\
**Category:** General\
**Created:** [April 20, 2020, 8:49pm UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403 "2020-04-20T20:49:08Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gabehaus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gabehaus/32/133285_2.png) [@Gabehaus](https://forum.freecodecamp.org/u/Gabehaus)\
**Post date:** [April 20, 2020, 8:49pm UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403/1 "2020-04-20T20:49:08Z")

</div>

Good day : )

I’m very confused about the following React code.  
I get data from an API successfully.  
I define a state property “foodName” and asign my API data to this property.  
Now I can set additional state properties to subproperties of this foodName property:  
this.setState({ measure: this.state.foodName.hints[0].measures[0].label });  
^This works as expected. I can render this state to the DOM and see my data.

BUT I can’t instead render my original API data (the returned data, dataJSON, that was assigned to the foodName property) to the DOM via:

{this.state.foodName.hints[0].measures[0].label}

^this throws an error of “Cannot read property ‘0’ of undefined”

Please explain if you can see what is going on. This is very confusing to me.

Thanks in advance

```auto

import React, { Component } from "react";

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      foodName: {},
      test: "state is posting successfully"
    };
    this.getData = this.getData.bind(this);
  }

  componentDidMount() {
    this.getData();
  }

  async getData() {
    let url =
      "https://api.edamam.com/api/food-database/parser?nutrition-type=logging&ingr=red%20apple&app_id=e9e86788&app_key=2c231e68f3531783f0fed14057834b04";

    let response = await fetch(url);
    //console.log(response);
    let dataJSON = await response.json();
    console.log(dataJSON.hints[0].measures[0].label);

    this.setState({ foodName: dataJSON });

    //console.log(this.state.foodName.hints[0].measures[0].label);
    this.setState({ measure: this.state.foodName.hints[0].measures[0].label });

  }

  render() {
    return (
      <div>
        <div>{this.state.test}</div>
        <div>{this.state.measure}</div> {/* This returns a value */}
        <div>{this.state.foodName.hints[0].measures[0].label}</div>
        {/* This doesn't return a value - error "Cannot read property '0' of undefined
App.render */}
      </div>
    );
  }
}

```

---

<div class="post-metadata">

**Author:** ![dareedyone](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dareedyone](https://forum.freecodecamp.org/u/dareedyone)\
**Post date:** [April 20, 2020, 9:25pm UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403/2 "2020-04-20T21:25:25Z")

</div>

Hello, have you try logging foodName to the console to see if its defined ?

---

<div class="post-metadata">

**Author:** ![AbdelliNasredine](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/abdellinasredine/32/119695_2.png) [@AbdelliNasredine](https://forum.freecodecamp.org/u/AbdelliNasredine)\
**Post date:** [April 20, 2020, 9:31pm UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403/3 "2020-04-20T21:31:58Z")

</div>

hi @Gabehaus  
I see that your getting JSON response from your API, why you are using two this.setStates, you could do just one, like this:

```auto
const data = { 
   foodName: dataJSON,
   measure : dataJSON.hints[0].measures[0].label
};
this.setState(data);

```

---

<div class="post-metadata">

**Author:** ![Gabehaus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gabehaus/32/133285_2.png) [@Gabehaus](https://forum.freecodecamp.org/u/Gabehaus)\
**Post date:** [April 20, 2020, 9:36pm UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403/4 "2020-04-20T21:36:59Z")

</div>

Logging foodName returns something like “cannot log object” as foodName should be an object

---

<div class="post-metadata">

**Author:** ![Gabehaus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gabehaus/32/133285_2.png) [@Gabehaus](https://forum.freecodecamp.org/u/Gabehaus)\
**Post date:** [April 20, 2020, 9:37pm UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403/5 "2020-04-20T21:37:15Z")

</div>

Yes, but that doesnt’ solve my problem

---

<div class="post-metadata">

**Author:** ![dareedyone](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dareedyone](https://forum.freecodecamp.org/u/dareedyone)\
**Post date:** [April 20, 2020, 9:41pm UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403/6 "2020-04-20T21:41:04Z")

</div>

exactly, so the values are not in foodName which explain why you are getting “cannot read property 0 of undefined”. console.log(typeof foodName). Lets see what type it is. From there we should know where to go and what is wrong.

---

<div class="post-metadata">

**Author:** ![Gabehaus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gabehaus/32/133285_2.png) [@Gabehaus](https://forum.freecodecamp.org/u/Gabehaus)\
**Post date:** [April 20, 2020, 9:54pm UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403/7 "2020-04-20T21:54:05Z")

</div>

typeof this.state.foodName = object

---

<div class="post-metadata">

**Author:** ![dareedyone](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dareedyone](https://forum.freecodecamp.org/u/dareedyone)\
**Post date:** [April 20, 2020, 9:55pm UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403/8 "2020-04-20T21:55:36Z")

</div>

It will really help to know what’s in the object. Any way i can get access to your code base ??

---

<div class="post-metadata">

**Author:** ![Gabehaus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gabehaus/32/133285_2.png) [@Gabehaus](https://forum.freecodecamp.org/u/Gabehaus)\
**Post date:** [April 20, 2020, 9:56pm UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403/9 "2020-04-20T21:56:20Z")

</div>

I don’t see how this works:  
this.setState({ measure: this.state.foodName.hints[0].measures[0].label });

but this doesn’t render:

```auto
render()
return 
<div>{this.state.foodName.hints[0].measures[0].label} </div>

```

---

<div class="post-metadata">

**Author:** ![Gabehaus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gabehaus/32/133285_2.png) [@Gabehaus](https://forum.freecodecamp.org/u/Gabehaus)\
**Post date:** [April 21, 2020, 12:07am UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403/10 "2020-04-21T00:07:13Z")

</div>

```auto
import React, { Component } from "react";

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      foodName: {},
      test: "state is posting successfully"
    };
    this.getData = this.getData.bind(this);
  }

  componentDidMount() {
    this.getData();
  }

  async getData() {
    let url =
      "https://api.edamam.com/api/food-database/parser?nutrition-type=logging&ingr=red%20apple&app_id=e9e86788&app_key=2c231e68f3531783f0fed14057834b04";

    let response = await fetch(url);
    //console.log(response);
    let dataJSON = await response.json();
    console.log(dataJSON.hints[0].measures[0].label);

    this.setState({ foodName: dataJSON });

    //console.log(this.state.foodName.hints[0].measures[0].label);
    this.setState({ measure: this.state.foodName.hints[0].measures[0].label });
    //console.log(this.state.foodName + "pebo");
    console.log(typeof this.state.foodName);
  }

  render() {
    return (
      <div>
        <div>{this.state.test}</div>
        <div>{this.state.measure}</div> {/* This returns a value */}{" "}
        {/* <div>{this.state.foodName.hints[0].measures[0].label}</div>This doesn't return a value - error "Cannot read property '0' of undefined
App.render */}
      </div>
    );
  }
}

```

---

<div class="post-metadata">

**Author:** ![dareedyone](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dareedyone](https://forum.freecodecamp.org/u/dareedyone)\
**Post date:** [April 21, 2020, 6:33am UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403/11 "2020-04-21T06:33:32Z")

</div>

I am able to go through your code now. It wasn’t working because what you were doing what wasn’t logical in react. You were trying to access nested objects or object in the dom which isn’t proper by react. All your accessing should be done in your logic like how you reassigned the part you need to another state “measure” or destructure the part you need to different states on mounting life cycle. Why you cant access objects in the dom is definitely because reacts assumes you want to render the property in the objects which is rather console fit for testing purposes rather than the dom (probably because of the complexity of keys and value pairs) if you think about it. Arrays (single or nested arrays that doesn’t involve object) can be used in the dom instead because it is simple for react to know you want to display it contents. if objects is involved in arrays then you will want to use array methods like filter or map to tell react dom what to do with e.g display it as ordered list or unordered. I hope this make sense. 🙂

---

<div class="post-metadata">

**Author:** ![nkwatra](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nkwatra/32/138063_2.png) [@nkwatra](https://forum.freecodecamp.org/u/nkwatra)\
**Post date:** [April 21, 2020, 7:58am UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403/12 "2020-04-21T07:58:13Z")

</div>

@Gabehaus You are calling getData() function in componentDidMount(). Actually how the [Component Lifecycle](https://reactjs.org/docs/react-component.html#the-component-lifecycle) works in react is, first the component is created (constructor called) and then it is mounted (for this purpose render() is called) and after that componentDidMount() is called. So your code is failing in the first render. This is because in the first render foodName is an empty object (since you initialised it to an empty object). So foodName.hints is undefined and an error occurs here. In subsequent renders however, after the data has been loaded, foodName.hints is defined and react successfully renders the component.  
So to solve this problem, you can create an attribute loading in this.state and set it to true. Then once the data is available, you can update state and set loading to false. Also in render(), you would have to conditionally render either loading indicator or data depending upon value of this.state.loading.  
Refer to below code for a correct implementation :

```auto
import React, { Component } from "react";

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      foodName: {},
      loading: true
    };
    this.getData = this.getData.bind(this);
  }

  componentDidMount() {
    this.getData();
  }

  async getData() {
    let url =
      "https://api.edamam.com/api/food-database/parser?nutrition-type=logging&ingr=red%20apple&app_id=e9e86788&app_key=2c231e68f3531783f0fed14057834b04";

    let response = await fetch(url);

    let dataJSON = await response.json();

    this.setState({ foodName: dataJSON, loading: false });
  }

  render() {
    return this.state.loading ? (
      <h1>Laoding...</h1>
    ) : (
      <div>{this.state.foodName.hints[0].measures[0].label}</div>
    );
  }
}

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:58pm UTC](https://forum.freecodecamp.org/t/react-state-confusion/376403/13 "2021-06-01T18:58:10Z")

</div>

Closed on June 1, 2021, 6:58pm UTC


