简体   繁体   English

如何更改 - 单选按钮 antd

[英]how to on change -radio button antd

i have question arr and foreche question i want to render the question and antd radio button answers.我有问题 arr 和 foreche 问题,我想呈现问题和 antd 单选按钮答案。

the problem is when i change any question, all the answer changed as well to the same answer that i change in the first question.问题是当我更改任何问题时,所有答案都更改为我在第一个问题中更改的相同答案。 there is any way to do key or id to the radio button and on change by that key, or any help??有什么方法可以对单选按钮执行键或 id 并通过该键进行更改,或者有任何帮助吗? this is my code:这是我的代码:

 import { Radio } from "antd";
 const { questions} = props;
 const [value, setValue] = useState(1);

 const onChange = e => {
        setValue(e.target.value);
};

{questions.map((question, i) => {                     
            return (<div><div>{question.Description}</div>
            <Radio.Group value={value} onChange={onChange} >
                 <Radio value={1}>Aי</Radio>
                 <Radio value={2}>B</Radio>
                 <Radio value={3}>C</Radio>
            </Radio.Group></div>) 
 })}

You must create a state of array or create a child component for radio buttons to has own state:您必须创建一个 state 数组或为单选按钮创建一个子组件以拥有自己的 state:

import { Radio } from "antd";
const { questions} = props;
const [value, setValue] = useState([]);

const onChange = (e, i) => {
    setValue(prevState => {
        const newState = [...prevState]; 
        newState[i] = e.target.value;
        return newState;
    });
};

{questions.map((question, i) => {                     
    return (<div><div>{question.Description}</div>
        <Radio.Group value={value[i]} onChange={e => onChange(e,i)} >
             <Radio value={1}>Aי</Radio>
             <Radio value={2}>B</Radio>
             <Radio value={3}>C</Radio>
        </Radio.Group></div>) 
})}

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

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