React antd 表单填充数据

问题描述 投票:0回答:1

尝试从给定的键值类型对象填充 Antd 表单项,以键作为标签,以值作为输入参数。我尝试了以下代码(也在网上搜索),但它不起作用,有人可以帮我吗?

import { Form, Input } from 'antd';
const onFinish = (values) => {
    console.log('Success:', values);
};
const onFinishFailed = (errorInfo) => {
    console.log('Failed:', errorInfo);
};
const data = {
    fname: 'First',
    lname: 'Last',
    age: '31'
};
const MyForm = () => (
    <>
        <h2>My Data</h2>
        <Form
            onFinish={onFinish}
            onFinishFailed={onFinishFailed}
            initialValues={data}
        >
            {Object.entries(data).forEach(([key, value]) => {
                <Form.Item label={key} name={key} >
                    <Input value={value} />
                </Form.Item>
            })}
        </Form>
    </>
);
export default MyForm;
javascript reactjs json antd
1个回答
1
投票

我认为你应该使用map而不是forEach,因为forEach不会返回任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.