React cannot read an object inside array (state)





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Relatively new to React, wrote a few components before (successfully), but this is the first time I'm getting something started from scratch. I use Rails with Cassandra to provide JSON responses, and everything on the back end side is working fine.



My data this.state.data looks like this:



0 {id: {n: 2.1751612473052575e+38}, email: "123@zeba.me", high_score: 73, shoe_size: 10.5, updated_at: "2018-11-06T01:23:36.611-08:00"}
1 {id: {n: 2.8024982600468778e+38}, email: "123@sotkamonlukio.fi", high_score: 13, shoe_size: 7.5, updated_at: "2018-11-06T01:24:55.791-08:00"}
2 {id: {n: 2.4227336868283995e+38}, email: "123@icloud.com", high_score: 99, shoe_size: 10.5, updated_at: "2018-11-06T01:24:07.858-08:00"}


And doing this.state.data[1] obviously returns



{id: {n: 2.8024982600468778e+38}, email: "123@sotkamonlukio.fi", high_score: 13, shoe_size: 7.5, updated_at: "2018-11-06T01:24:55.791-08:00"}


However, this.state.data[1].email throws this



TypeError: undefined is not an object (evaluating 'this.state.data[1].email')


What can I do to access email and the rest of the data?





full component code:



import React, { Component } from 'react';

export default class Table extends Component {
constructor() {
super();
this.state = {
data: ,
}
}

componentDidMount() {
fetch('http://localhost:3000/api/users')
.then(response => response.json())
.then(data => this.setState({ data }))
}

render() {
return(
<div className="table">
<h1>LOL</h1>
<p>{this.state.data[1].email}</p>
</div>
)
}
}









share|improve this question




















  • 1





    please add all the component code . you probably didn't initialised the state and render the component

    – Naor Tedgi
    Nov 17 '18 at 1:06











  • @NaorTedgi sure, updated the post.

    – Peter Chudinov
    Nov 17 '18 at 1:11











  • yap it is what I suspect. check the answer below

    – Naor Tedgi
    Nov 17 '18 at 1:26




















0















Relatively new to React, wrote a few components before (successfully), but this is the first time I'm getting something started from scratch. I use Rails with Cassandra to provide JSON responses, and everything on the back end side is working fine.



My data this.state.data looks like this:



0 {id: {n: 2.1751612473052575e+38}, email: "123@zeba.me", high_score: 73, shoe_size: 10.5, updated_at: "2018-11-06T01:23:36.611-08:00"}
1 {id: {n: 2.8024982600468778e+38}, email: "123@sotkamonlukio.fi", high_score: 13, shoe_size: 7.5, updated_at: "2018-11-06T01:24:55.791-08:00"}
2 {id: {n: 2.4227336868283995e+38}, email: "123@icloud.com", high_score: 99, shoe_size: 10.5, updated_at: "2018-11-06T01:24:07.858-08:00"}


And doing this.state.data[1] obviously returns



{id: {n: 2.8024982600468778e+38}, email: "123@sotkamonlukio.fi", high_score: 13, shoe_size: 7.5, updated_at: "2018-11-06T01:24:55.791-08:00"}


However, this.state.data[1].email throws this



TypeError: undefined is not an object (evaluating 'this.state.data[1].email')


What can I do to access email and the rest of the data?





full component code:



import React, { Component } from 'react';

export default class Table extends Component {
constructor() {
super();
this.state = {
data: ,
}
}

componentDidMount() {
fetch('http://localhost:3000/api/users')
.then(response => response.json())
.then(data => this.setState({ data }))
}

render() {
return(
<div className="table">
<h1>LOL</h1>
<p>{this.state.data[1].email}</p>
</div>
)
}
}









share|improve this question




















  • 1





    please add all the component code . you probably didn't initialised the state and render the component

    – Naor Tedgi
    Nov 17 '18 at 1:06











  • @NaorTedgi sure, updated the post.

    – Peter Chudinov
    Nov 17 '18 at 1:11











  • yap it is what I suspect. check the answer below

    – Naor Tedgi
    Nov 17 '18 at 1:26
















0












0








0








Relatively new to React, wrote a few components before (successfully), but this is the first time I'm getting something started from scratch. I use Rails with Cassandra to provide JSON responses, and everything on the back end side is working fine.



My data this.state.data looks like this:



0 {id: {n: 2.1751612473052575e+38}, email: "123@zeba.me", high_score: 73, shoe_size: 10.5, updated_at: "2018-11-06T01:23:36.611-08:00"}
1 {id: {n: 2.8024982600468778e+38}, email: "123@sotkamonlukio.fi", high_score: 13, shoe_size: 7.5, updated_at: "2018-11-06T01:24:55.791-08:00"}
2 {id: {n: 2.4227336868283995e+38}, email: "123@icloud.com", high_score: 99, shoe_size: 10.5, updated_at: "2018-11-06T01:24:07.858-08:00"}


And doing this.state.data[1] obviously returns



{id: {n: 2.8024982600468778e+38}, email: "123@sotkamonlukio.fi", high_score: 13, shoe_size: 7.5, updated_at: "2018-11-06T01:24:55.791-08:00"}


However, this.state.data[1].email throws this



TypeError: undefined is not an object (evaluating 'this.state.data[1].email')


What can I do to access email and the rest of the data?





full component code:



import React, { Component } from 'react';

export default class Table extends Component {
constructor() {
super();
this.state = {
data: ,
}
}

componentDidMount() {
fetch('http://localhost:3000/api/users')
.then(response => response.json())
.then(data => this.setState({ data }))
}

render() {
return(
<div className="table">
<h1>LOL</h1>
<p>{this.state.data[1].email}</p>
</div>
)
}
}









share|improve this question
















Relatively new to React, wrote a few components before (successfully), but this is the first time I'm getting something started from scratch. I use Rails with Cassandra to provide JSON responses, and everything on the back end side is working fine.



My data this.state.data looks like this:



0 {id: {n: 2.1751612473052575e+38}, email: "123@zeba.me", high_score: 73, shoe_size: 10.5, updated_at: "2018-11-06T01:23:36.611-08:00"}
1 {id: {n: 2.8024982600468778e+38}, email: "123@sotkamonlukio.fi", high_score: 13, shoe_size: 7.5, updated_at: "2018-11-06T01:24:55.791-08:00"}
2 {id: {n: 2.4227336868283995e+38}, email: "123@icloud.com", high_score: 99, shoe_size: 10.5, updated_at: "2018-11-06T01:24:07.858-08:00"}


And doing this.state.data[1] obviously returns



{id: {n: 2.8024982600468778e+38}, email: "123@sotkamonlukio.fi", high_score: 13, shoe_size: 7.5, updated_at: "2018-11-06T01:24:55.791-08:00"}


However, this.state.data[1].email throws this



TypeError: undefined is not an object (evaluating 'this.state.data[1].email')


What can I do to access email and the rest of the data?





full component code:



import React, { Component } from 'react';

export default class Table extends Component {
constructor() {
super();
this.state = {
data: ,
}
}

componentDidMount() {
fetch('http://localhost:3000/api/users')
.then(response => response.json())
.then(data => this.setState({ data }))
}

render() {
return(
<div className="table">
<h1>LOL</h1>
<p>{this.state.data[1].email}</p>
</div>
)
}
}






reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 1:11







Peter Chudinov

















asked Nov 17 '18 at 1:01









Peter ChudinovPeter Chudinov

112




112








  • 1





    please add all the component code . you probably didn't initialised the state and render the component

    – Naor Tedgi
    Nov 17 '18 at 1:06











  • @NaorTedgi sure, updated the post.

    – Peter Chudinov
    Nov 17 '18 at 1:11











  • yap it is what I suspect. check the answer below

    – Naor Tedgi
    Nov 17 '18 at 1:26
















  • 1





    please add all the component code . you probably didn't initialised the state and render the component

    – Naor Tedgi
    Nov 17 '18 at 1:06











  • @NaorTedgi sure, updated the post.

    – Peter Chudinov
    Nov 17 '18 at 1:11











  • yap it is what I suspect. check the answer below

    – Naor Tedgi
    Nov 17 '18 at 1:26










1




1





please add all the component code . you probably didn't initialised the state and render the component

– Naor Tedgi
Nov 17 '18 at 1:06





please add all the component code . you probably didn't initialised the state and render the component

– Naor Tedgi
Nov 17 '18 at 1:06













@NaorTedgi sure, updated the post.

– Peter Chudinov
Nov 17 '18 at 1:11





@NaorTedgi sure, updated the post.

– Peter Chudinov
Nov 17 '18 at 1:11













yap it is what I suspect. check the answer below

– Naor Tedgi
Nov 17 '18 at 1:26







yap it is what I suspect. check the answer below

– Naor Tedgi
Nov 17 '18 at 1:26














3 Answers
3






active

oldest

votes


















0














The problem is that, when your component starts the rendering, the this.state.data[1].email wasn't loaded already. Just check if the data was already loaded, like below:



render(){
if(this.state.data.length > 0)
return(
<div className="table">
<h1>LOL</h1>
<p>{this.state.data[1].email}</p>
</div>
)
else return <div/>
}


The other possible solution is to use the map function. It will only render something when the array it's filled. The code is below:



render(){
return(
<div className="table">
<h1>LOL</h1>
{ this.state.data.map(user => <p>{user.email}</p>) }
</div>
)
}


For each user in the data array, the component will render a tag <p> with the email. When there's nothing in the array, the component will render nothing.






share|improve this answer


























  • Is there a way to only render the component if data is fully loaded without flooding the code with an extra if statement? I have never seen such construction in production-ready code.

    – Peter Chudinov
    Nov 17 '18 at 1:31











  • The if is already only rendering the component if the data is fully loaded. But, I'll add a possible solution to my answer.

    – mthrsj
    Nov 17 '18 at 10:14











  • @PeterChudinov answer edited

    – mthrsj
    Nov 17 '18 at 10:19



















0














its a common error the request didn't complete before the render method is called just add an if statement






  render () {
const { data } = this.state
return (
<div className='table'>
<h1>LOL</h1>
{data.length > 0 && <p>{data[1].email}</p>}
</div>
)
}








share|improve this answer


























  • Is there a way to avoid such an if statement? It seems to be complicating the code for no reason.

    – Peter Chudinov
    Nov 17 '18 at 1:30











  • you always have to return value from the render method. there is all kind of ways to avoid it both its depends on your app component hierarchy

    – Naor Tedgi
    Nov 17 '18 at 1:36











  • i think this is the same issue here stackoverflow.com/questions/45808247/… see Appunni M answer

    – Naor Tedgi
    Nov 17 '18 at 1:40













  • @PeterChudinov i Update to something more elegant

    – Naor Tedgi
    Nov 17 '18 at 1:47



















0














This.state.data initially set to an empty array. So when your component first time renders it will be an empty array.



In componentDidMount you are making an api call and assigning api response to the state data. So this method gets called after first render



The reason you get that issue because you are accessing 1st index from this.state.data array which is obviously an empty array at the time of first render. So what you have to do is



Change



   <p>{this.state.data[1].email}</p>


To



    <p>{this.state.data.length > 0 && this.state.data[1].email}</p>


Or do iterate the array and render p elements



   {this.state.data.length > 0 && this.state.data.map(d => (  
<p key={d.id}>d.email}</p>
)}


If you don’t have unique id per object in array then use index as key






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347251%2freact-cannot-read-an-object-inside-array-state%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The problem is that, when your component starts the rendering, the this.state.data[1].email wasn't loaded already. Just check if the data was already loaded, like below:



    render(){
    if(this.state.data.length > 0)
    return(
    <div className="table">
    <h1>LOL</h1>
    <p>{this.state.data[1].email}</p>
    </div>
    )
    else return <div/>
    }


    The other possible solution is to use the map function. It will only render something when the array it's filled. The code is below:



    render(){
    return(
    <div className="table">
    <h1>LOL</h1>
    { this.state.data.map(user => <p>{user.email}</p>) }
    </div>
    )
    }


    For each user in the data array, the component will render a tag <p> with the email. When there's nothing in the array, the component will render nothing.






    share|improve this answer


























    • Is there a way to only render the component if data is fully loaded without flooding the code with an extra if statement? I have never seen such construction in production-ready code.

      – Peter Chudinov
      Nov 17 '18 at 1:31











    • The if is already only rendering the component if the data is fully loaded. But, I'll add a possible solution to my answer.

      – mthrsj
      Nov 17 '18 at 10:14











    • @PeterChudinov answer edited

      – mthrsj
      Nov 17 '18 at 10:19
















    0














    The problem is that, when your component starts the rendering, the this.state.data[1].email wasn't loaded already. Just check if the data was already loaded, like below:



    render(){
    if(this.state.data.length > 0)
    return(
    <div className="table">
    <h1>LOL</h1>
    <p>{this.state.data[1].email}</p>
    </div>
    )
    else return <div/>
    }


    The other possible solution is to use the map function. It will only render something when the array it's filled. The code is below:



    render(){
    return(
    <div className="table">
    <h1>LOL</h1>
    { this.state.data.map(user => <p>{user.email}</p>) }
    </div>
    )
    }


    For each user in the data array, the component will render a tag <p> with the email. When there's nothing in the array, the component will render nothing.






    share|improve this answer


























    • Is there a way to only render the component if data is fully loaded without flooding the code with an extra if statement? I have never seen such construction in production-ready code.

      – Peter Chudinov
      Nov 17 '18 at 1:31











    • The if is already only rendering the component if the data is fully loaded. But, I'll add a possible solution to my answer.

      – mthrsj
      Nov 17 '18 at 10:14











    • @PeterChudinov answer edited

      – mthrsj
      Nov 17 '18 at 10:19














    0












    0








    0







    The problem is that, when your component starts the rendering, the this.state.data[1].email wasn't loaded already. Just check if the data was already loaded, like below:



    render(){
    if(this.state.data.length > 0)
    return(
    <div className="table">
    <h1>LOL</h1>
    <p>{this.state.data[1].email}</p>
    </div>
    )
    else return <div/>
    }


    The other possible solution is to use the map function. It will only render something when the array it's filled. The code is below:



    render(){
    return(
    <div className="table">
    <h1>LOL</h1>
    { this.state.data.map(user => <p>{user.email}</p>) }
    </div>
    )
    }


    For each user in the data array, the component will render a tag <p> with the email. When there's nothing in the array, the component will render nothing.






    share|improve this answer















    The problem is that, when your component starts the rendering, the this.state.data[1].email wasn't loaded already. Just check if the data was already loaded, like below:



    render(){
    if(this.state.data.length > 0)
    return(
    <div className="table">
    <h1>LOL</h1>
    <p>{this.state.data[1].email}</p>
    </div>
    )
    else return <div/>
    }


    The other possible solution is to use the map function. It will only render something when the array it's filled. The code is below:



    render(){
    return(
    <div className="table">
    <h1>LOL</h1>
    { this.state.data.map(user => <p>{user.email}</p>) }
    </div>
    )
    }


    For each user in the data array, the component will render a tag <p> with the email. When there's nothing in the array, the component will render nothing.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 17 '18 at 10:18

























    answered Nov 17 '18 at 1:20









    mthrsjmthrsj

    1,6541724




    1,6541724













    • Is there a way to only render the component if data is fully loaded without flooding the code with an extra if statement? I have never seen such construction in production-ready code.

      – Peter Chudinov
      Nov 17 '18 at 1:31











    • The if is already only rendering the component if the data is fully loaded. But, I'll add a possible solution to my answer.

      – mthrsj
      Nov 17 '18 at 10:14











    • @PeterChudinov answer edited

      – mthrsj
      Nov 17 '18 at 10:19



















    • Is there a way to only render the component if data is fully loaded without flooding the code with an extra if statement? I have never seen such construction in production-ready code.

      – Peter Chudinov
      Nov 17 '18 at 1:31











    • The if is already only rendering the component if the data is fully loaded. But, I'll add a possible solution to my answer.

      – mthrsj
      Nov 17 '18 at 10:14











    • @PeterChudinov answer edited

      – mthrsj
      Nov 17 '18 at 10:19

















    Is there a way to only render the component if data is fully loaded without flooding the code with an extra if statement? I have never seen such construction in production-ready code.

    – Peter Chudinov
    Nov 17 '18 at 1:31





    Is there a way to only render the component if data is fully loaded without flooding the code with an extra if statement? I have never seen such construction in production-ready code.

    – Peter Chudinov
    Nov 17 '18 at 1:31













    The if is already only rendering the component if the data is fully loaded. But, I'll add a possible solution to my answer.

    – mthrsj
    Nov 17 '18 at 10:14





    The if is already only rendering the component if the data is fully loaded. But, I'll add a possible solution to my answer.

    – mthrsj
    Nov 17 '18 at 10:14













    @PeterChudinov answer edited

    – mthrsj
    Nov 17 '18 at 10:19





    @PeterChudinov answer edited

    – mthrsj
    Nov 17 '18 at 10:19













    0














    its a common error the request didn't complete before the render method is called just add an if statement






      render () {
    const { data } = this.state
    return (
    <div className='table'>
    <h1>LOL</h1>
    {data.length > 0 && <p>{data[1].email}</p>}
    </div>
    )
    }








    share|improve this answer


























    • Is there a way to avoid such an if statement? It seems to be complicating the code for no reason.

      – Peter Chudinov
      Nov 17 '18 at 1:30











    • you always have to return value from the render method. there is all kind of ways to avoid it both its depends on your app component hierarchy

      – Naor Tedgi
      Nov 17 '18 at 1:36











    • i think this is the same issue here stackoverflow.com/questions/45808247/… see Appunni M answer

      – Naor Tedgi
      Nov 17 '18 at 1:40













    • @PeterChudinov i Update to something more elegant

      – Naor Tedgi
      Nov 17 '18 at 1:47
















    0














    its a common error the request didn't complete before the render method is called just add an if statement






      render () {
    const { data } = this.state
    return (
    <div className='table'>
    <h1>LOL</h1>
    {data.length > 0 && <p>{data[1].email}</p>}
    </div>
    )
    }








    share|improve this answer


























    • Is there a way to avoid such an if statement? It seems to be complicating the code for no reason.

      – Peter Chudinov
      Nov 17 '18 at 1:30











    • you always have to return value from the render method. there is all kind of ways to avoid it both its depends on your app component hierarchy

      – Naor Tedgi
      Nov 17 '18 at 1:36











    • i think this is the same issue here stackoverflow.com/questions/45808247/… see Appunni M answer

      – Naor Tedgi
      Nov 17 '18 at 1:40













    • @PeterChudinov i Update to something more elegant

      – Naor Tedgi
      Nov 17 '18 at 1:47














    0












    0








    0







    its a common error the request didn't complete before the render method is called just add an if statement






      render () {
    const { data } = this.state
    return (
    <div className='table'>
    <h1>LOL</h1>
    {data.length > 0 && <p>{data[1].email}</p>}
    </div>
    )
    }








    share|improve this answer















    its a common error the request didn't complete before the render method is called just add an if statement






      render () {
    const { data } = this.state
    return (
    <div className='table'>
    <h1>LOL</h1>
    {data.length > 0 && <p>{data[1].email}</p>}
    </div>
    )
    }








      render () {
    const { data } = this.state
    return (
    <div className='table'>
    <h1>LOL</h1>
    {data.length > 0 && <p>{data[1].email}</p>}
    </div>
    )
    }





      render () {
    const { data } = this.state
    return (
    <div className='table'>
    <h1>LOL</h1>
    {data.length > 0 && <p>{data[1].email}</p>}
    </div>
    )
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 17 '18 at 1:45

























    answered Nov 17 '18 at 1:16









    Naor TedgiNaor Tedgi

    1,17811023




    1,17811023













    • Is there a way to avoid such an if statement? It seems to be complicating the code for no reason.

      – Peter Chudinov
      Nov 17 '18 at 1:30











    • you always have to return value from the render method. there is all kind of ways to avoid it both its depends on your app component hierarchy

      – Naor Tedgi
      Nov 17 '18 at 1:36











    • i think this is the same issue here stackoverflow.com/questions/45808247/… see Appunni M answer

      – Naor Tedgi
      Nov 17 '18 at 1:40













    • @PeterChudinov i Update to something more elegant

      – Naor Tedgi
      Nov 17 '18 at 1:47



















    • Is there a way to avoid such an if statement? It seems to be complicating the code for no reason.

      – Peter Chudinov
      Nov 17 '18 at 1:30











    • you always have to return value from the render method. there is all kind of ways to avoid it both its depends on your app component hierarchy

      – Naor Tedgi
      Nov 17 '18 at 1:36











    • i think this is the same issue here stackoverflow.com/questions/45808247/… see Appunni M answer

      – Naor Tedgi
      Nov 17 '18 at 1:40













    • @PeterChudinov i Update to something more elegant

      – Naor Tedgi
      Nov 17 '18 at 1:47

















    Is there a way to avoid such an if statement? It seems to be complicating the code for no reason.

    – Peter Chudinov
    Nov 17 '18 at 1:30





    Is there a way to avoid such an if statement? It seems to be complicating the code for no reason.

    – Peter Chudinov
    Nov 17 '18 at 1:30













    you always have to return value from the render method. there is all kind of ways to avoid it both its depends on your app component hierarchy

    – Naor Tedgi
    Nov 17 '18 at 1:36





    you always have to return value from the render method. there is all kind of ways to avoid it both its depends on your app component hierarchy

    – Naor Tedgi
    Nov 17 '18 at 1:36













    i think this is the same issue here stackoverflow.com/questions/45808247/… see Appunni M answer

    – Naor Tedgi
    Nov 17 '18 at 1:40







    i think this is the same issue here stackoverflow.com/questions/45808247/… see Appunni M answer

    – Naor Tedgi
    Nov 17 '18 at 1:40















    @PeterChudinov i Update to something more elegant

    – Naor Tedgi
    Nov 17 '18 at 1:47





    @PeterChudinov i Update to something more elegant

    – Naor Tedgi
    Nov 17 '18 at 1:47











    0














    This.state.data initially set to an empty array. So when your component first time renders it will be an empty array.



    In componentDidMount you are making an api call and assigning api response to the state data. So this method gets called after first render



    The reason you get that issue because you are accessing 1st index from this.state.data array which is obviously an empty array at the time of first render. So what you have to do is



    Change



       <p>{this.state.data[1].email}</p>


    To



        <p>{this.state.data.length > 0 && this.state.data[1].email}</p>


    Or do iterate the array and render p elements



       {this.state.data.length > 0 && this.state.data.map(d => (  
    <p key={d.id}>d.email}</p>
    )}


    If you don’t have unique id per object in array then use index as key






    share|improve this answer




























      0














      This.state.data initially set to an empty array. So when your component first time renders it will be an empty array.



      In componentDidMount you are making an api call and assigning api response to the state data. So this method gets called after first render



      The reason you get that issue because you are accessing 1st index from this.state.data array which is obviously an empty array at the time of first render. So what you have to do is



      Change



         <p>{this.state.data[1].email}</p>


      To



          <p>{this.state.data.length > 0 && this.state.data[1].email}</p>


      Or do iterate the array and render p elements



         {this.state.data.length > 0 && this.state.data.map(d => (  
      <p key={d.id}>d.email}</p>
      )}


      If you don’t have unique id per object in array then use index as key






      share|improve this answer


























        0












        0








        0







        This.state.data initially set to an empty array. So when your component first time renders it will be an empty array.



        In componentDidMount you are making an api call and assigning api response to the state data. So this method gets called after first render



        The reason you get that issue because you are accessing 1st index from this.state.data array which is obviously an empty array at the time of first render. So what you have to do is



        Change



           <p>{this.state.data[1].email}</p>


        To



            <p>{this.state.data.length > 0 && this.state.data[1].email}</p>


        Or do iterate the array and render p elements



           {this.state.data.length > 0 && this.state.data.map(d => (  
        <p key={d.id}>d.email}</p>
        )}


        If you don’t have unique id per object in array then use index as key






        share|improve this answer













        This.state.data initially set to an empty array. So when your component first time renders it will be an empty array.



        In componentDidMount you are making an api call and assigning api response to the state data. So this method gets called after first render



        The reason you get that issue because you are accessing 1st index from this.state.data array which is obviously an empty array at the time of first render. So what you have to do is



        Change



           <p>{this.state.data[1].email}</p>


        To



            <p>{this.state.data.length > 0 && this.state.data[1].email}</p>


        Or do iterate the array and render p elements



           {this.state.data.length > 0 && this.state.data.map(d => (  
        <p key={d.id}>d.email}</p>
        )}


        If you don’t have unique id per object in array then use index as key







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 '18 at 3:35









        Hemadri DasariHemadri Dasari

        10.4k32458




        10.4k32458






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347251%2freact-cannot-read-an-object-inside-array-state%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python