如何解决1 not === 1反应

时间:2019-05-11 07:56:06

标签: reactjs

state.item.type: 1,然后在箭头功能if this.state.item.type===1 return video else err.中,但输出err

这是我正在使用的React Gallery的类型,用于定义该项目是哪种媒体

状态:

state = {
    item: [
      {
        id: 0,
        src: "https://images.unsplash.com/flagged/photo-1551706646-9c816bfbff8c?ixlib=rb-1.2.1&auto=format&fit=crop&w=1567&q=80",
        h: "h",
        p: "p",
        divid: "id1",
        type: 1,
      },
   ]
}

箭头功能:

type = (e) => {
    if (this.state.item.type === 1) {
      return 'video';
    } else {
      return 'err';
    }
  }

输出:

<h1>{type}</h1>

我希望在h1中输出视频,但实际输出是err

1 个答案:

答案 0 :(得分:1)

  1. this.state.item.type更改为this.state.item[0].type
  2. 您必须编写<h1>{this.type()}</h1>才能立即调用函数。
  3. 它可能具有不同的状态和功能名称。