I'm writing a React App that gets JSON data from an API and shows some of it's contents.
This is the data it gets:
{"id":6,"name":"5 Storey I=0.7","Value":1344981250,"NSt":5,"jRatio":"[0.2,0.4,0.4]","jEDR":"[0.02,0.1,0.5,1]"}
And this is the App:
class App extends React.Component{
constructor(props){
super(props);
this.state={
data: [],
isLoading: false,
error: null,
};
}
componentDidMount(){
this.setState({isLoading: true});
axios.get(API)
.then(response => console.log(response.data.Value))
.then(response => this.setState({data: response.data, isLoading: false}))
.catch(response => this.setState({error: response.error, isLoading: false}));
}
render(){
return (
<div>
<p>{this.state.error}</p>
<p>{this.state.isLoading ? 'Loading...':'Loaded'}</p>
<ul>{this.state.data.Value.toString()}</ul>
</div>
)
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
I get the Value "1344981250" in console but the page throws this error:
TypeError: this.state.data.Value is undefined
and in console:
The development server has disconnected. Refresh the page if necessary.
I also tried this:
this.state.data.map(obj => <li key={obj.toString()}>{obj.toString()}</li>)
and this time without any error, nothing shows up on the page. (this one works for arrays of objects but shows nothing when it's just one object)
So what did I miss?
.then(responsetheresponseis undefined, since that's whatconsole.log()"returns".