Unable to access nested object in React












0














I am trying to access single elements within an object and I keep getting an error.



Here is my React Component:






import React, { Component } from "react";
import { fetchCoin } from "../redux/actions";
import { connect } from "react-redux";

class ViewCoin extends Component {
componentDidMount() {
this.props.fetchCoin(this.props.match.params.symbol);
}

render() {
console.log("Props:", this.props.coin);
return (
<div>
<h2>View Item Page</h2>
</div>
);
}
}

const mapStateToProps = state => {
return {
coin: state.coins.coin
};
};

export default connect(mapStateToProps, { fetchCoin })(ViewCoin);





This code returns the following object:






{
"BTC": {
"urls": {
"website": [
"https://bitcoin.org/"
],
"twitter": ,
"reddit": [
"https://reddit.com/r/bitcoin"
],
"message_board": [
"https://bitcointalk.org"
],
"announcement": ,
"chat": ,
"explorer": [
"https://blockchain.info/",
"https://live.blockcypher.com/btc/",
"https://blockchair.com/bitcoin/blocks"
],
"source_code": [
"https://github.com/bitcoin/"
]
},
"logo": "https://s2.coinmarketcap.com/static/img/coins/64x64/1.png",
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"slug": "bitcoin",
"date_added": "2013-04-28T00:00:00.000Z",
"tags": [
"mineable"
],
"category": "coin"
}
}





When trying to access the coin name, for example, I issue the command:



console.log(this.props.coin.BTC.name);



I get the error: Cannot read property 'BTC' of undefined



I tested accessing values in this object outside of React and I am able to access the different values. How would one access a nested object like this using React.



Thanks in advance.










share|improve this question






















  • looks fine, but you need a constructor. constructor(props){ super(props) }
    – Ziyo
    Nov 21 at 1:55
















0














I am trying to access single elements within an object and I keep getting an error.



Here is my React Component:






import React, { Component } from "react";
import { fetchCoin } from "../redux/actions";
import { connect } from "react-redux";

class ViewCoin extends Component {
componentDidMount() {
this.props.fetchCoin(this.props.match.params.symbol);
}

render() {
console.log("Props:", this.props.coin);
return (
<div>
<h2>View Item Page</h2>
</div>
);
}
}

const mapStateToProps = state => {
return {
coin: state.coins.coin
};
};

export default connect(mapStateToProps, { fetchCoin })(ViewCoin);





This code returns the following object:






{
"BTC": {
"urls": {
"website": [
"https://bitcoin.org/"
],
"twitter": ,
"reddit": [
"https://reddit.com/r/bitcoin"
],
"message_board": [
"https://bitcointalk.org"
],
"announcement": ,
"chat": ,
"explorer": [
"https://blockchain.info/",
"https://live.blockcypher.com/btc/",
"https://blockchair.com/bitcoin/blocks"
],
"source_code": [
"https://github.com/bitcoin/"
]
},
"logo": "https://s2.coinmarketcap.com/static/img/coins/64x64/1.png",
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"slug": "bitcoin",
"date_added": "2013-04-28T00:00:00.000Z",
"tags": [
"mineable"
],
"category": "coin"
}
}





When trying to access the coin name, for example, I issue the command:



console.log(this.props.coin.BTC.name);



I get the error: Cannot read property 'BTC' of undefined



I tested accessing values in this object outside of React and I am able to access the different values. How would one access a nested object like this using React.



Thanks in advance.










share|improve this question






















  • looks fine, but you need a constructor. constructor(props){ super(props) }
    – Ziyo
    Nov 21 at 1:55














0












0








0







I am trying to access single elements within an object and I keep getting an error.



Here is my React Component:






import React, { Component } from "react";
import { fetchCoin } from "../redux/actions";
import { connect } from "react-redux";

class ViewCoin extends Component {
componentDidMount() {
this.props.fetchCoin(this.props.match.params.symbol);
}

render() {
console.log("Props:", this.props.coin);
return (
<div>
<h2>View Item Page</h2>
</div>
);
}
}

const mapStateToProps = state => {
return {
coin: state.coins.coin
};
};

export default connect(mapStateToProps, { fetchCoin })(ViewCoin);





This code returns the following object:






{
"BTC": {
"urls": {
"website": [
"https://bitcoin.org/"
],
"twitter": ,
"reddit": [
"https://reddit.com/r/bitcoin"
],
"message_board": [
"https://bitcointalk.org"
],
"announcement": ,
"chat": ,
"explorer": [
"https://blockchain.info/",
"https://live.blockcypher.com/btc/",
"https://blockchair.com/bitcoin/blocks"
],
"source_code": [
"https://github.com/bitcoin/"
]
},
"logo": "https://s2.coinmarketcap.com/static/img/coins/64x64/1.png",
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"slug": "bitcoin",
"date_added": "2013-04-28T00:00:00.000Z",
"tags": [
"mineable"
],
"category": "coin"
}
}





When trying to access the coin name, for example, I issue the command:



console.log(this.props.coin.BTC.name);



I get the error: Cannot read property 'BTC' of undefined



I tested accessing values in this object outside of React and I am able to access the different values. How would one access a nested object like this using React.



Thanks in advance.










share|improve this question













I am trying to access single elements within an object and I keep getting an error.



Here is my React Component:






import React, { Component } from "react";
import { fetchCoin } from "../redux/actions";
import { connect } from "react-redux";

class ViewCoin extends Component {
componentDidMount() {
this.props.fetchCoin(this.props.match.params.symbol);
}

render() {
console.log("Props:", this.props.coin);
return (
<div>
<h2>View Item Page</h2>
</div>
);
}
}

const mapStateToProps = state => {
return {
coin: state.coins.coin
};
};

export default connect(mapStateToProps, { fetchCoin })(ViewCoin);





This code returns the following object:






{
"BTC": {
"urls": {
"website": [
"https://bitcoin.org/"
],
"twitter": ,
"reddit": [
"https://reddit.com/r/bitcoin"
],
"message_board": [
"https://bitcointalk.org"
],
"announcement": ,
"chat": ,
"explorer": [
"https://blockchain.info/",
"https://live.blockcypher.com/btc/",
"https://blockchair.com/bitcoin/blocks"
],
"source_code": [
"https://github.com/bitcoin/"
]
},
"logo": "https://s2.coinmarketcap.com/static/img/coins/64x64/1.png",
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"slug": "bitcoin",
"date_added": "2013-04-28T00:00:00.000Z",
"tags": [
"mineable"
],
"category": "coin"
}
}





When trying to access the coin name, for example, I issue the command:



console.log(this.props.coin.BTC.name);



I get the error: Cannot read property 'BTC' of undefined



I tested accessing values in this object outside of React and I am able to access the different values. How would one access a nested object like this using React.



Thanks in advance.






import React, { Component } from "react";
import { fetchCoin } from "../redux/actions";
import { connect } from "react-redux";

class ViewCoin extends Component {
componentDidMount() {
this.props.fetchCoin(this.props.match.params.symbol);
}

render() {
console.log("Props:", this.props.coin);
return (
<div>
<h2>View Item Page</h2>
</div>
);
}
}

const mapStateToProps = state => {
return {
coin: state.coins.coin
};
};

export default connect(mapStateToProps, { fetchCoin })(ViewCoin);





import React, { Component } from "react";
import { fetchCoin } from "../redux/actions";
import { connect } from "react-redux";

class ViewCoin extends Component {
componentDidMount() {
this.props.fetchCoin(this.props.match.params.symbol);
}

render() {
console.log("Props:", this.props.coin);
return (
<div>
<h2>View Item Page</h2>
</div>
);
}
}

const mapStateToProps = state => {
return {
coin: state.coins.coin
};
};

export default connect(mapStateToProps, { fetchCoin })(ViewCoin);





{
"BTC": {
"urls": {
"website": [
"https://bitcoin.org/"
],
"twitter": ,
"reddit": [
"https://reddit.com/r/bitcoin"
],
"message_board": [
"https://bitcointalk.org"
],
"announcement": ,
"chat": ,
"explorer": [
"https://blockchain.info/",
"https://live.blockcypher.com/btc/",
"https://blockchair.com/bitcoin/blocks"
],
"source_code": [
"https://github.com/bitcoin/"
]
},
"logo": "https://s2.coinmarketcap.com/static/img/coins/64x64/1.png",
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"slug": "bitcoin",
"date_added": "2013-04-28T00:00:00.000Z",
"tags": [
"mineable"
],
"category": "coin"
}
}





{
"BTC": {
"urls": {
"website": [
"https://bitcoin.org/"
],
"twitter": ,
"reddit": [
"https://reddit.com/r/bitcoin"
],
"message_board": [
"https://bitcointalk.org"
],
"announcement": ,
"chat": ,
"explorer": [
"https://blockchain.info/",
"https://live.blockcypher.com/btc/",
"https://blockchair.com/bitcoin/blocks"
],
"source_code": [
"https://github.com/bitcoin/"
]
},
"logo": "https://s2.coinmarketcap.com/static/img/coins/64x64/1.png",
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"slug": "bitcoin",
"date_added": "2013-04-28T00:00:00.000Z",
"tags": [
"mineable"
],
"category": "coin"
}
}






javascript node.js reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 1:40









Kareem Phillip-Jackson

205




205












  • looks fine, but you need a constructor. constructor(props){ super(props) }
    – Ziyo
    Nov 21 at 1:55


















  • looks fine, but you need a constructor. constructor(props){ super(props) }
    – Ziyo
    Nov 21 at 1:55
















looks fine, but you need a constructor. constructor(props){ super(props) }
– Ziyo
Nov 21 at 1:55




looks fine, but you need a constructor. constructor(props){ super(props) }
– Ziyo
Nov 21 at 1:55












1 Answer
1






active

oldest

votes


















1














What's likely happening is the first time that component renders coin is undefined. When the action returns, the props are updated, the component re-renders and coin is logged out.



Do something like this in render and it should work:



this.props.coin && console.log('coin name:', this.props.coin.BTC.name)





share|improve this answer





















  • This does work, thank you!
    – Kareem Phillip-Jackson
    Nov 22 at 16:10










  • glad to hear it!
    – jsw324
    Nov 24 at 12:29











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%2f53404157%2funable-to-access-nested-object-in-react%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














What's likely happening is the first time that component renders coin is undefined. When the action returns, the props are updated, the component re-renders and coin is logged out.



Do something like this in render and it should work:



this.props.coin && console.log('coin name:', this.props.coin.BTC.name)





share|improve this answer





















  • This does work, thank you!
    – Kareem Phillip-Jackson
    Nov 22 at 16:10










  • glad to hear it!
    – jsw324
    Nov 24 at 12:29
















1














What's likely happening is the first time that component renders coin is undefined. When the action returns, the props are updated, the component re-renders and coin is logged out.



Do something like this in render and it should work:



this.props.coin && console.log('coin name:', this.props.coin.BTC.name)





share|improve this answer





















  • This does work, thank you!
    – Kareem Phillip-Jackson
    Nov 22 at 16:10










  • glad to hear it!
    – jsw324
    Nov 24 at 12:29














1












1








1






What's likely happening is the first time that component renders coin is undefined. When the action returns, the props are updated, the component re-renders and coin is logged out.



Do something like this in render and it should work:



this.props.coin && console.log('coin name:', this.props.coin.BTC.name)





share|improve this answer












What's likely happening is the first time that component renders coin is undefined. When the action returns, the props are updated, the component re-renders and coin is logged out.



Do something like this in render and it should work:



this.props.coin && console.log('coin name:', this.props.coin.BTC.name)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 2:22









jsw324

39818




39818












  • This does work, thank you!
    – Kareem Phillip-Jackson
    Nov 22 at 16:10










  • glad to hear it!
    – jsw324
    Nov 24 at 12:29


















  • This does work, thank you!
    – Kareem Phillip-Jackson
    Nov 22 at 16:10










  • glad to hear it!
    – jsw324
    Nov 24 at 12:29
















This does work, thank you!
– Kareem Phillip-Jackson
Nov 22 at 16:10




This does work, thank you!
– Kareem Phillip-Jackson
Nov 22 at 16:10












glad to hear it!
– jsw324
Nov 24 at 12:29




glad to hear it!
– jsw324
Nov 24 at 12:29


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53404157%2funable-to-access-nested-object-in-react%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

Wiesbaden

Marschland

Dieringhausen