简体   繁体   English

antd 4: 用defaultValue为Select设置默认值,不能使用initialValue

[英]antd 4: setting a default value for Select with defaultValue, can't use initialValue

In our rather complex form we have a dynamic form field (similar to the example in the antd documentation, except using a Select field).在我们相当复杂的表单中,我们有一个动态表单字段(类似于 antd 文档中的示例,除了使用 Select 字段)。 We use initialValue to feed our Form data from the database, now we want to have our Select fields, which are added dynamically, to have a default value.我们使用 initialValue 从数据库中提供表单数据,现在我们希望动态添加的 Select 字段具有默认值。

The problem exists in the fact that it isn't possible to add an initialValue to fields that haven't rendered yet + form doesn't know how many dynamic Select fields will be added.问题在于,无法向尚未呈现的字段添加 initialValue + form 不知道将添加多少个动态 Select 字段。

So instinctively I resorted to the defaultValue prop on the Select box, which in my eyes should just work, but it doesn't.因此,我本能地使用了 Select 框上的 defaultValue 属性,在我看来它应该可以正常工作,但它没有。 (in antd 4 there is no fieldDescriptor with a defaultValue) (在 antd 4 中没有带 defaultValue 的 fieldDescriptor)

Maybe this example will explain better what I'm trying to say: https://codesandbox.io/s/thirsty-hamilton-m7bmc也许这个例子会更好地解释我想说的: https : //codesandbox.io/s/thirsty-hamilton-m7bmc

If you add a field in the example and hit submit, it will complain the field is required.如果您在示例中添加一个字段并点击提交,它会抱怨该字段是必需的。 However it certainly does have a value in there, but apparently not for the Form state.然而,它确实在那里有一个值,但显然不是 Form 状态。

I hope someone else has encountered a similar issue我希望其他人也遇到过类似的问题

Your Select components need VALUE to be defined.您的 Select 组件需要定义VALUE I see you have defaultValue, but they also need such properties as VALUE , and ONCHANGE .我看到您有 defaultValue,但他们还需要诸如VALUEONCHANGE 之类的属性。 When you add them the select will start to work properly.当您添加它们时,选择将开始正常工作。

1). 1)。 It is better to define default value inside VALUE property, so that if nothing is selected by user (selected value is undefined), the value will default to whatever you mention in the following condition with ternary operator:最好在 VALUE 属性中定义默认值,这样如果用户没有选择任何内容(所选值未定义),则该值将默认为您在以下条件中使用三元运算符提及的任何内容:

value={selectedValue ?值={选定值? selectedValue : defaultValue}. selectedValue:默认值}。

2). 2)。 In onChange property you need to pass value and update the state with that value:onChange属性中,您需要传递并使用该值更新状态:

onChange={(value) => this.updateSelectedValue(value)} onChange={(value) => this.updateSelectedValue(value)}

import React, { PureComponent } from "react";
import { Form, Select } from "antd";

const { Option } = Select;
export default class DynamicFieldSet extends PureComponent {
    state = {
        selectedValue: undefined,
    }

    updateSelectedValue = value => this.setState({ selectedValue: value })

    render() {
        const { selectedValue } = this.state;
        return (
            <Form>
                    <Form.Item>
                        <Select
                            value={selectedValue ? selectedValue : "TAG"}
                            onChange={(value) => this.updateSelectedValue(value)}
                        >
                            <Option value="MARKER">Marker</Option>
                            <Option value="TAG">Tag</Option>
                            <Option value="FIELD">Field</Option>
                        </Select>
                    </Form.Item>
            </Form>
          );
    }
}

暂无
暂无

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

相关问题 AntD Rate DefaultValue 没有得到值 - AntD Rate DefaultValue Doesn't Get the Value 警告:使用 &#39;defaultValue&#39; 或 &#39;value&#39; 道具<select>而不是设置“选择”<option> - Warning: Use the 'defaultValue' or 'value' props on <select> instead of setting 'selected' on <option> 警告:使用 `defaultValue` 或 `value` 道具<select>而不是设置`selected`<option> - Warning: Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option> 使用 `defaultValue` 或 `value` 道具<select>而不是设置`selected`<option> - Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option> Antd Select 不设置初始值 - Antd Select not setting initial value 默认值或值不适用于 FormItem ANTD - defaultValue or value not working on FormItem ANTD 当我更改选择时,我收到:&#39;使用 `defaultValue` 或 `value` 道具<select>而不是设置`selected`<option> &#39; - When i change the select i receive: 'Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option>' 反应-警告:在上使用`defaultValue`或`value`道具 <select>而不是将“ selected”设置为on <option> - React - Warning: Use the `defaultValue` or `value` props on <select> instead of setting `selected` on <option> antd 4 表单中多选的默认值 - default value for multiple select in form antd 4 <select>应该有 {defaultValue} 或 {value} 而不是设置 {selected} - <select> should have {defaultValue} or {value} instead of setting {selected}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM