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

Reactjs TypeError:无法读取属性';地图';未定义的类型(axios=>;getData=>;setState=>;.map=>;return elmItem=>;错误),reactjs,axios,typeerror,setstate,array-map,Reactjs,Axios,Typeerror,Setstate,Array Map,我应该使用分页吗?每当我点击这个按钮时,它就会被扔掉 TypeError:无法读取未定义的属性“map” 这两者之间有什么关系吗 axios=>getData=>setState=>map=>return-Item 谢谢大家! class ContentComponent extends Component { constructor(props) { super(props); this.state = { items: [], }; } co

我应该使用分页吗?每当我点击这个按钮时,它就会被扔掉

TypeError:无法读取未定义的属性“map”

这两者之间有什么关系吗

axios=>getData=>setState=>map=>return-Item

谢谢大家!

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>
class ContentComponent扩展组件{
建造师(道具){
超级(道具);
此.state={
项目:[],
};
}
componentDidMount(){
axios
.get(“http://localhost:4000/api/todos")
.then(res=>this.setState({items:res.data.result}))
.catch(错误=>console.log(错误));
}
handleDelete=值=>{
axios
.删除(`http://localhost:4000/api/todos/${value}`)
.then(res=>this.setState({items:res.data.result}))
.catch(错误=>console.log(错误));
};
render(){
设{items}=this.state;
const elm item=items.map((项,索引)=>{
返回;
});}
{elmItem}

在控制台浏览器中使用console.log()调试您的handleDelete函数

  • 检查您的值参数是否正确,并传递正确的值
  • 检查你的回答
  • 要了解有关axios的更多信息,请点击以下链接

    要了解地图,请点击下面的链接


    你在
    res.data.result中得到了什么
    我当然明白我在axios中得到了什么。删除先生。所以问题在几分钟前就解决了,非常感谢!我正在使用axios.get after axios.delete来设置项的状态,现在我面临着异步的问题,当我单击delete时,它没有设置状态,我的第二次单击将使它设置状态。确保您从res.data.result和res.data.result获取的数据必须是数组的形式,如果您想用map函数迭代该数据。到目前为止,有一个例子可以告诉我,先生,您能给我一些简单的例子吗?
    
     handleDelete = value => {
        console.log(value); // check paremeter value here
        axios
        .delete(`http://localhost:4000/api/todos/${value}`)
        .then(res => {
          console.log(res.data.result) // check is data return in the array form so you iterate with the map function.
         this.setState({items: res.data.result})
        })
        .catch(error => console.log(error));
      };