【问题标题】:React hooks radio button list gives warning: Each child in a list should have a unique "key" propReact hooks 单选按钮列表给出警告:列表中的每个孩子都应该有一个唯一的“key”道具
【发布时间】:2021-02-12 05:06:24
【问题描述】:

尽管带有单选按钮的表单按预期工作(检查所选选项并显示对应的组件),但我不断收到唯一键道具警告。
我正在使用对象数组将数据提供给渲染函数并返回无线电输入 jsx。

代码如下: (主窗体)

import React, {useState} from 'react'

import FormOperationOptions from './FormOptions'
import UserConfig from './user-config/UserConfig'



const Form = () => {

const[operation, setOperation] = useState('')

const selectOperationOnChange = (event) =>{
  setOperation(event.target.value);
} 
 
const operationsListKey =  FormOperationOptions.map((operations)=>{
  return operations.id})


const renderAllOperations = (value, key) => {
    return(
<div>
      <input type='radio' 
      
      key={key} 
      value={value} 
      checked={value===operation? true:false}
      readOnly
      />
      <label htmlFor={value}>{value}</label><br/>
  </div>
    )
  }

const selectedOperation = () => {
  if(operation === 'userConfig'){return(<div><UserConfig /></div>)}
}

return(
  <div>
<h3>Choose operation type:</h3>
<form 
    onChange={selectOperationOnChange}
>
    {FormOperationOptions.map((operations)=>{
    return renderAllOperations(operations.selectedOption, operationsListKey);})}
</form>          
    {selectedOperation()}
  </div>
  )
}
export default Form 

(表单的数据来自哪里)

const FormOptions = [
  {
    id:1,
    selectedOption: 'userConfig',
  },
  {
    id:2,
    selectedOption: 'gitIgnore',
  },
  {
    id:3,
    selectedOption: 'localRepository',  
  },
  {
    id:4,
    selectedOption: 'remoteRepository',   
  },
]

export default FormOptions

和 UserConfig 组件:

import React from 'react'

const UserConfig = () =>{

  return(
    <div> UserConfig component </div>
  )
}

export default UserConfig

感谢您的宝贵时间和意见:)

【问题讨论】:

  • checked={value===operation? true:false}等于只写checked={value===operation}

标签: reactjs react-hooks radiobuttonlist


【解决方案1】:

你能试试这个吗:

return(
  <div>
<h3>Choose operation type:</h3>
<form 
    onChange={selectOperationOnChange}
>
    {FormOperationOptions.map((operations)=>{
    return renderAllOperations(operations.selectedOption, operations.id);})}
</form>          
    {selectedOperation()}
  </div>
  )

这样,每个映射操作都会以 id 作为键。在您的代码中,您将 operationsListKey 作为第二个参数传递,这是一个函数本身,它在每次 map 函数迭代时返回每个操作的 id。

让我知道它是否有效。

【讨论】:

  • 嗨,Maximiliano,我确实尝试过,并将值作为关键参数传递给 renderAllOperations 函数。
    const renderAllOperations = (value, key) =&gt; { console.log(key) return( &lt;div&gt; &lt;input type='radio' key={key} value={value} checked={value===operation? true:false} readOnly /&gt; &lt;label htmlFor={value}&gt;{value}&lt;/label&gt;&lt;br/&gt; &lt;/div&gt; ) }
    不过仍然出现错误。
【解决方案2】:

您在错误的元素中添加了“键”。它应该添加在“div”而不是“input”上。

const renderAllOperations = (value, key) => {
  return(
    <div key={key}>
    <input type='radio'      
      value={value} 
      checked={value===operation? true:false}
      readOnly
    />
    <label htmlFor={value}>{value}</label><br/>
    </div>
  )
}

如果'input'需要key,你可以像下面这样传递另一个

const renderAllOperations = (value, key) => {
  return(
    <div key={key}>
    <input type='radio'
      key={`radio_${key}`}      
      value={value} 
      checked={value===operation? true:false}
      readOnly
    />
    <label htmlFor={value}>{value}</label><br/>
    </div>
  )
}

【讨论】:

  • 非常感谢 Paveloosha,它现在可以在没有警告的情况下工作,我真的以为关键在输入元素中,而不是在包含的 div 上。
猜你喜欢
  • 2020-09-06
  • 1970-01-01
  • 2021-07-03
  • 2023-02-01
  • 2021-07-20
  • 2020-03-01
  • 1970-01-01
  • 2023-02-17
  • 1970-01-01
相关资源
最近更新 更多