我正在面对像TypeError这样的问题:this.state.monsters.map不是一个函数。我已经给出了this.state = {monsters:[]}在提供了上述状态之后,我仍然面对着那个地图一个功能。...
import React, { Component } from 'react'
import './App.css';
class App extends Component {
constructor (){
super ()
this.state = {
monsters : []
};
}
componentDidMount() {
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json)
.then(users => this.setState({monsters : users}))
}
render() {
return (
<div className="App">
{this.state.monsters.map(monster =>(
<h1 key={monster.id} > {monster.name} </h1>
))}
</div>
);
}
}
export default App;
答案 0 :(得分:1)
Inovke response.json
componentDidMount() {
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json()) // added parentheses
.then(monsters => this.setState({ monsters }))
}