Reactjs TypeError:无法读取属性';handleDelete';未定义的

Reactjs TypeError:无法读取属性';handleDelete';未定义的,reactjs,Reactjs,我正在学习React&below代码,来自“”的代码抛出了如下奇怪的错误,请帮助 代码 handleDelete = () => { console.log("Delete"); }; render() { console.log("this outside", this); return ( <div> {this.state.counters.map(function (cnt) {

我正在学习React&below代码,来自“”的代码抛出了如下奇怪的错误,请帮助

代码

handleDelete = () => {
console.log("Delete");
};


render() {
    console.log("this outside", this);
    return (
      <div>
        {this.state.counters.map(function (cnt) {
          //console.log(cnt);
          console.log("this inside", this);
          //new properties / props are added here
          return (
            <Counter
              key={cnt.id}
              onDelete={this.handleDelete}
              value={cnt.value}
              id={cnt.id}
            />
          );
        })}
      </div>
    );
  }
handleDelete=()=>{
控制台日志(“删除”);
};
render(){
log(“这个在外面”,这个);
返回(
{this.state.counters.map(函数(cnt)){
//控制台日志(cnt);
log(“这里面”,这里面);
//此处添加了新的属性/道具
返回(
);
})}
);
}
输出


因为
onDelete={this.handleDelete}
在范围
{this.state.counters.map(函数(cnt){

你应使用:

        {this.state.counters.map(cnt) => { // autobind using arrow function
          //console.log(cnt);
          console.log("this inside", this);
          //new properties / props are added here
          return (
            <Counter
              key={cnt.id}
              onDelete={this.handleDelete}
              value={cnt.value}
              id={cnt.id}
            />
          );
        })}
{this.state.counters.map(cnt)=>{//autobind using arrow函数
//控制台日志(cnt);
log(“这里面”,这里面);
//此处添加了新的属性/道具
返回(
);
})}

因为
onDelete={this.handleDelete}
在范围
{this.state.counters.map(函数(cnt){

你应使用:

        {this.state.counters.map(cnt) => { // autobind using arrow function
          //console.log(cnt);
          console.log("this inside", this);
          //new properties / props are added here
          return (
            <Counter
              key={cnt.id}
              onDelete={this.handleDelete}
              value={cnt.value}
              id={cnt.id}
            />
          );
        })}
{this.state.counters.map(cnt)=>{//autobind using arrow函数
//控制台日志(cnt);
log(“这里面”,这里面);
//此处添加了新的属性/道具
返回(
);
})}

我认为,因为您使用了单词函数-this指的是cnt而不是类。尝试将map函数更改为arrow函数我认为因为您使用了单词函数-this指的是cnt而不是类。尝试将map函数更改为arrow函数function@vikramvi:同时读取差异b介于箭头函数和常规函数之间。您将了解上下文
this
的工作原理。@vikramvi:还将阅读箭头函数和常规函数之间的区别。您将了解上下文
this
的工作原理。