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
答案 0 :(得分:1)
this.state.item.type
更改为this.state.item[0].type
。<h1>{this.type()}</h1>
才能立即调用函数。