TypeError:无法读取未定义的属性'map'(axios => getData => setState => .map => return elmItem =>错误)

问题描述 投票:0回答:1

我应该使用分页吗?每当我单击此按钮<Delete>时,它就会抛出

TypeError:无法读取未定义的属性'map'。

之间是否有任何关联>

axios => getData => setState => .map => return elmItem

谢谢!

class ContentComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: [],
    };
  }

  componentDidMount() {
    axios
      .get("http://localhost:4000/api/todos")
      .then(res => this.setState({ items: res.data.result }))
      .catch(error => console.log(error));
  }

  handleDelete = value => {
    axios
    .delete(`http://localhost:4000/api/todos/${value}`)
    .then(res => this.setState({items: res.data.result}))
    .catch(error => console.log(error));
  };

  render() {
    let { items } = this.state;
    const elmItem = items.map((item, index) => {
      return <TableComponent item={item} key={index} index={index} handleDelete={this.handleDelete}/>;
    });}
    <table className="table">
          <tbody>{elmItem}</tbody>
    </table>

我应该使用分页吗?每当我单击此按钮时,抛出该TypeError:无法读取未定义的属性'map'。 axios => getData => ...

reactjs axios typeerror setstate array-map
1个回答
0
投票

使用console.log()调试handleDelete

© www.soinside.com 2019 - 2024. All rights reserved.