Primereact: Warning: Encountered two children with the same key,

对着背影说爱祢 提交于 2021-02-11 14:42:30

问题


I am using primereact dropdown component https://primefaces.org/primereact/showcase/#/dropdown

But getting this warning:-

react_devtools_backend.js:2450 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.
in ul (created by DropdownPanel)
in div (created by DropdownPanel)

this.cities = [
            { name: 'Apple', code: '1' },
            { name: 'Rome', code: '2' },
            { name: 'Rome', code: '3' },
            { name: 'Paris', code: '4' },
            { name: 'Paris', code: '5' }
        ];

<Dropdown 
    value={this.state.selectedCity1} 
    options={this.cities} 
    onChange={this.onCityChange} 
    optionLabel="name" 
    placeholder="Select a City" />

How to fix this in primereact dropdown.


回答1:


Maybe you should to add optionValue="code"?

<Dropdown
  value={this.state.selectedCity1}
  options={this.cities}
  onChange={this.onCityChange}
  optionLabel="name"
  optionValue="code"
  placeholder="Select a City"
/>


来源:https://stackoverflow.com/questions/64771089/primereact-warning-encountered-two-children-with-the-same-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!