В настоящее время я создаю хоккейное приложение с использованием API из НХЛ.
Я новичок в React и всегда получаю ошибку item.map не является функцией.
import React, {Component} from 'react';
class Api extends Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
items: []
};
}
componentDidMount() {
fetch("https://statsapi.web.nhl.com/api/v1/divisions/1")
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
items: result.items
});
},
// Note: it's important to handle errors here
// instead of a catch() block so that we don't swallow
// exceptions from actual bugs in components.
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
}
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>
<li key={item.id}>{item.name}</li>
</ul>
);
}
}
}
export default Api
Ошибка определенно в функции рендера, но я не могу ее исправить!
Спасибо за вашу помощь,
Джон