首页 > 解决方案 > 道具“价值”的值无效标签

问题描述

我收到了无法解决的警告:

value标签上的道具值无效。要么将其从元素中移除,要么传递一个字符串或数值以将其保留在 DOM 中。详情

以下是我正在使用的代码:

<FormItem validateStatus={NameError ? "error" : ""} help={NameError || ""}>
  {getFieldDecorator("Name", {
    initialValue: (()=>{this.state.Data.Name}),
    rules: [{ required: true, message: "Please input the component name!" }]
  })(
    <Input
      className="form-control"
      type="text"
      name="Name"
      defaultValue={this.state.Data.Name}
      onChange={this.onChange}
    />
  )}
</FormItem>

我的打字稿界面如下所示:

export interface IFieldEdition{
    Data:IFieldData
}

export interface IFieldData{
    Id?:number,
    Name?:string,
    Value?:string,
    Description?:string,
    CreatedDate?:Date,
    CreatedBy?:string,
    StatusId?: number
}

我该如何解决这个问题?有什么线索吗?

标签: reactjsantd

解决方案


我看到你正在使用 antd 表单。来自antd form官方文档

被getFieldDecorator包裹后,表单控件中会添加value(或valuePropName定义的其他属性)onChange(或trigger定义的其他属性)props,表单数据的流动将由Form处理,这将导致:

您不应该手动调用 setState,请使用 this.props.form.setFieldsValue 以编程方式更改值。

您使用initialValue: (()=>{this.state.Data.Name}, 调用 setState 可能是您收到此错误的原因。


推荐阅读