TypeError:this.state.monsters.map不是函数

时间:2019-08-29 05:14:58

标签: reactjs

我正在面对像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;

1 个答案:

答案 0 :(得分:1)

Inovke response.json

componentDidMount() {
  fetch('https://jsonplaceholder.typicode.com/users')
    .then(response => response.json()) // added parentheses
    .then(monsters => this.setState({ monsters }))
}