I am pretty new to ReactJS and am trying to figure out why I am not able to display the fetched data. Checked the data in Postman and it is as expected so I am sure there is something syntactical I am missing. I didnt put the key value in this post.
import React, { Component } from 'react'
class Stockstuff extends Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
items: []
};
}
componentDidMount() {
fetch("https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=TSLA&apikey=???")
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
items: result.items
});
},
(error) => {
this.setState({
isLoaded: true,
error : true
});
}
)
}
render() {
const { error, isLoaded, items } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<ul>
{items.map(item => (
<li key={item.name}>
{item.name} {item.price}
</li>
))}
</ul>
);
}
}
}
export default Stockstuff
What is the fault?
error.???. @Ross Lamon, What is the expected payload?itemsexist on the response and it is an iterable object. The code is correct. the issue is not about ReactJS.