简体   繁体   English

Ant Design Form 组件不显示输入值

[英]Ant Design Form component not displaying input value

I have a multi-step form (wizard) consisting of several components, which all store the input data in a Redux reducer.我有一个由多个组件组成的多步骤表单(向导),它们都将输入数据存储在 Redux 减速器中。 To create the form itself, I use an antd Form component.为了创建表单本身,我使用了一个 antd Form 组件。 The issue is that if there's an already existing value (for example, the user typed it in, switched away from this step and switched back), it doesn't get displayed in the Input component: the component still shows the placeholder text.问题是,如果存在一个已经存在的值(例如,用户输入它,从这一步切换回并切换回来),它不会显示在 Input 组件中:该组件仍然显示占位符文本。 What am I doing wrong?我究竟做错了什么?

Source code:源代码:

const dispatch = useDispatch();

const layout = {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 },
    };
const [form] = Form.useForm();

<Form {...layout} form={form}>
                    <Form.Item
                            label="Name"
                            name="Name"
                            rules={[{ required: true }]}
                    >
                        <Input
                            placeholder={"Please enter a name"}
                            value={name}
                            addonBefore={name}
                            onChange={(event) => dispatch({type: WIZARD_SET_NAME, name: event.target.value})}
                        />
                    </Form.Item>
</Form>

This is not a problem with Redux itself: when console.logging or outputting the name property, it stores the input value the way it should.这不是 Redux 本身的问题:当 console.logging 或输出 name 属性时,它以它应该的方式存储输入值。

when you are returning to your previous screen your form is being re-rendered .当您返回上一个屏幕时,您的表单正在重新呈现。 you have to load the values of form from redux .您必须从 redux 加载 form 的值。

<Input
        placeholder={"Please enter a name"}
        value={GET_THE_VALUE_FROM_STORE}.   ///-> see here
        addonBefore={name}
        onChange={(event) => dispatch({type: WIZARD_SET_NAME, name: event.target.value})}
/>

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

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