【发布时间】:2018-10-19 02:52:44
【问题描述】:
我得到了这个:
警告:数组或迭代器中的每个子元素都应该有一个唯一的“key”属性。
这是我的下拉组件代码:
render() {
let dropDownClasses = [classes["im-dropdown"], classes[this.state.size], classes[this.state.dropStyle], classes[this.status]];
let itemcount = -1;
let options;
if(this.state.options !== undefined){
options = this.state.options.map(option => {
itemcount++;
return(<div><li key ={'opt_'+this.state.name+'_'+itemcount} role="option" aria-selected="false" value={option.value} tabIndex={itemcount} onClick={this.handleSelect.bind(this, this.state.name, option.value, option.text)}> {option.text} </li></div>)
});
}
return (
<div className={dropDownClasses.join(' ')}>
<button aria-haspopup="listbox" aria-expanded="false" onClick={this.handleOpen}>
<span>{this.state.label}</span><span className={classes["caret"]}><i className="fa fa-angle-down"></i></span>
</button>
<ul className={classes["options"]} role="listbox" aria-labelledby={"im-drop_" +this.state.size+"_"+this.state.dropStyle} >
<span key ={'opt_'+this.state.name+'_'+itemcount}>{options} </span>
</ul>
</div>
);
}
我通过另一个组件将它称为 SideDrawer.js,如下所示:
<div>
<span className ={classes["filter"]}><strong>FILTER BY</strong></span>
</div>
<Dropdown key={'opt_'+ clientDropdown.label} label={clientDropdown.label} name={clientDropdown.name} options={clientDropdown.options} action={props.formHandler}/>
<Dropdown label={typeDropdown.label} name={typeDropdown.name} options={typeDropdown.options} action={props.formHandler}/>
<Dropdown label={categoryDropdown.label} name={categoryDropdown.name} options={categoryDropdown.options} action={props.formHandler}/>
</div>
警告似乎即将到来,因为我在单个 div 中多次调用 Dropdown 组件(更像是一个项目数组),从而导致 Key 警告。我尝试使用“key”作为道具,但它没有_
【问题讨论】:
-
请完成最后一个短语(我在缺少的地方添加了“_”)。我还为您改进了帖子的格式,请在您的下一个问题和/或答案中这样做。最好的问候
标签: reactjs