问题:
在做前端项目时使用react+ant design使用<Select>标签出现警告如下:
Warning: Encountered two children with the same key, `.$高峰出车率`. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.
程序能够正常运行,但是测试时会出现多余option,
时常会添加相同option(明显:高峰出车率(%)是多余的);在每次出现的多余的option时,都会有上面的警告出现。
原因:
是由于option中有重复的选项,当每次刷新时由于重复的选项没有删除,会保留(这是react的锅,嗯。。。我是这么认为的)。
解决:
只要去掉重复的选项即可。解决es6对象数组,根据某个对象的属性去重方法如下:
let res = new Map();//CONTRASTNAME为对象的属性
let arr_file=payload.data.filter((item)=>!res.has(item.CONTRASTNAME) && res.set(item.CONTRASTNAME, 1))//过滤重复的数据
总结:这个问题应该是可以避免的 ,数据在后台应该需要控制,而不是在前端控制。