admin管理员组文章数量:1328400
I have dynamic data that radio draw.
There is an id of an active radio, which also es dynamically.
How to pare id and install cheked with Radio.Group. And in the end I need to get the radio values in the form This code does not work correctly.
dynamiclyData could be much bigger
const dynamiclyData = [
{ id: 1, value: "A" },
{ id: 2, value: "B" },
{ id: 3, value: "C" },
{ id: 4, value: "D" }
];
const idChekedFromRequest = 2;
const App = () => (
<Form onFinish={val => console.log("val", val)}>
<Form.Item name="radio">
<Radio.Group name="radiogroup">
{dynamiclyData.map(({ value, id }) => {
return (
<Radio key={id} checked={id === idChekedFromRequest}>
{value}
</Radio>
);
})}
</Radio.Group>
<Button htmlType="submit">submit</Button>
</Form.Item>
</Form>
);
CodeSanbox
I have dynamic data that radio draw.
There is an id of an active radio, which also es dynamically.
How to pare id and install cheked with Radio.Group. And in the end I need to get the radio values in the form This code does not work correctly.
dynamiclyData could be much bigger
const dynamiclyData = [
{ id: 1, value: "A" },
{ id: 2, value: "B" },
{ id: 3, value: "C" },
{ id: 4, value: "D" }
];
const idChekedFromRequest = 2;
const App = () => (
<Form onFinish={val => console.log("val", val)}>
<Form.Item name="radio">
<Radio.Group name="radiogroup">
{dynamiclyData.map(({ value, id }) => {
return (
<Radio key={id} checked={id === idChekedFromRequest}>
{value}
</Radio>
);
})}
</Radio.Group>
<Button htmlType="submit">submit</Button>
</Form.Item>
</Form>
);
CodeSanbox
Share Improve this question asked Jul 15, 2020 at 9:55 JuniorrrrrJuniorrrrr 1692 gold badges4 silver badges12 bronze badges2 Answers
Reset to default 4import React,{useState} from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Radio, Form, Button } from "antd";
const dynamiclyData = [
{ id: 1, value: "A" },
{ id: 2, value: "B" },
{ id: 3, value: "C" },
{ id: 4, value: "D" }
];
const App = () => {
const [idChekedFromRequest,setIdChekedFromRequest] = useState(2);
return (
<Form onFinish={val => console.log("val", val)}>
<Form.Item name="radio">
{/* <Radio.Group name="radiogroup"> */}
{dynamiclyData.map(({value, id}) => {
return (
<Radio key={id} onChange={()=>setIdChekedFromRequest(id)} checked={id===idChekedFromRequest}>
{value}
</Radio>
);
})}
{/* </Radio.Group> */}
<Button htmlType="submit">submit</Button>
</Form.Item>
</Form>
)
}
ReactDOM.render(<App />, document.getElementById("container"));
Might not be relevant now, but this can be fixed by applying the default value to Radio.Group. Check more details here https://ant.design/ponents/radio
<Radio.Group defaultValue={'no'}>
<Radio value={'yes'}>Yes</Radio>
<Radio value={'no'}>No</Radio>
</Radio.Group>
本文标签: javascriptHow to set checked with RadioGroup AntdStack Overflow
版权声明:本文标题:javascript - How to set checked with Radio.Group Antd? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742222741a2435555.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论