【发布时间】: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