如何解决错误“无法读取未定义的属性'setState'”

时间:2018-11-06 19:27:36

标签: javascript html json reactjs redux

更新1:

我更新了小提琴,但仍然遇到错误 _this.state.concat不是函数,请https://codesandbox.io/s/40mmrl9059

当我单击卡片中的“收藏夹”图标时,该卡片应显示在外部的“收藏夹”选项卡中。为此,我试图将值从子级传递给父级,所以我在handleClick方法中使用setState。

但是现在我遇到了错误

  

无法读取未定义的属性'setState'

您能告诉我如何解决吗?

(在下面提供我的代码段和沙箱。 -卡代码位于real-card.js中,而标签代码位于tab-demo.js中)

https://codesandbox.io/s/40mmrl9059

state = {
  value: 0,
  top: false,
  left: false,
  bottom: false,
  right: false,
  favorites: []
};

// props
handleClick(prop) {
  console.log("actualCard--->");
  //console.log("event.currentTarget--->", currentTarget.relatedTarget);
  this.setState({ favorites: this.state.concat(prop) });
}



<IconButton
  // onClick={this.handleClickOpen}
  onClick={this.handleClick}
  aria-label="Add to favorites"
>

<Tabs
  value={value}
  onChange={this.handleChange}
  scrollable
  scrollButtons="on"
  indicatorColor="primary"
  textColor="primary"
>
<Tab label="Search" icon={<PhoneIcon />} />
<Tab
  favorites={favorites}
  label="Favorites"
  icon={<FavoriteIcon />}
 />

1 个答案:

答案 0 :(得分:1)

您需要将this绑定到事件处理程序

尝试使用箭头功能,例如handleClick = (prop) => {//code}

相关问题