简体   繁体   English

React Semantic UI-在下拉菜单中为选项添加键

[英]React Semantic UI - add key to options in Dropdown menu

I have this Dropdown menu instance: 我有这个下拉菜单实例:

      <Dropdown
        selection
        options={this.state.options}
        search
        value={value}
        onChange={this.handleChange}
        onSearchChange={this.handleSearchChange}
      />

and when my backend returns response, which is then set as state and it is structured like this: 当我的后端返回响应时,将其设置为状态,其结构如下:

"options": [
    {
      "text": "New York,All Airports (NYC) , USA",
      "value": "NYC"
    },
    {
      "text": "New York,Newark Liberty Intl (EWR), USA",
      "value": "EWR"
    },
    {
      "text": "New York,John F Kennedy (JFK), USA",
      "value": "JFK"
    },
    {
      "text": "New York,La Guardia (LGA), USA",
      "value": "LGA"
    }
  ]

...I get this warning: ...我得到这个警告:

Warning: flattenChildren(...): Encountered two children with the same key, 1:$BLZ . 警告:flattenChildren(...):遇到两个具有相同键1:$BLZ孩子。 Child keys must be unique; 子密钥必须唯一。 when two children share a key, only the first child will be used. 当两个孩子共享一个密钥时,将仅使用第一个孩子。

in select (created by Dropdown)
in div (created by Dropdown)
in Dropdown (created by SearchForm)

How do I add keys to these elements to prevent this warning? 如何为这些元素添加键以防止出现此警告?

So looking at the code for the Semantic UI source for the dropdown component, the render options function converts your passed in options into a array of DropdownItem components: 因此,查看下拉组件的语义UI源代码,呈现选项功能会将您传入的选项转换为DropdownItem组件数组:

renderOptions = () => {
    const { multiple, search, noResultsMessage } = this.props
    const { selectedIndex, value } = this.state
    const options = this.getMenuOptions()

    if (noResultsMessage !== null && search && _.isEmpty(options)) {
      return <div className='message'>{noResultsMessage}</div>
    }

    const isActive = multiple
      ? optValue => _.includes(value, optValue)
      : optValue => optValue === value

    return _.map(options, (opt, i) => (
      <DropdownItem
        key={`${opt.value}-${i}`}
        active={isActive(opt.value)}
        onClick={this.handleItemClick}
        selected={selectedIndex === i}
        {...opt}
        // Needed for handling click events on disabled items
        style={{ ...opt.style, pointerEvents: 'all' }}
      />
    ))
  }

the key for this array is set by taking the value prop and appending the index to it: 通过采用值prop并为其添加索引来设置此数组的键:

key={`${opt.value}-${i}`}

which should always be unique since the index is used but there is another part of the code for hidden inputs 由于使用了索引,它应该始终是唯一的,但是代码的另一部分用于隐藏输入

 renderHiddenInput = () => {
    debug('renderHiddenInput()')
    const { value } = this.state
    const { multiple, name, options, selection } = this.props
    debug(`name:      ${name}`)
    debug(`selection: ${selection}`)
    debug(`value:     ${value}`)
    if (!selection) return null

    // a dropdown without an active item will have an empty string value
    return (
      <select type='hidden' aria-hidden='true' name={name} value={value} multiple={multiple}>
        <option value='' />
        {_.map(options, option => (
          <option key={option.value} value={option.value}>{option.text}</option>
        ))}
      </select>
    )
  }

in this one the key is set to only the value, not the value plus index. 在此密钥中,键仅设置为值,而不是值加索引。

 <option key={option.value} value={option.value}>{option.text}</option>

this might be your problem, if you have duplicate values then the key will not be unique. 这可能是您的问题,如果您有重复的值,那么键将不是唯一的。 Double check the options list to make sure you don't have duplicate values. 仔细检查选项列表,确保没有重复的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM