You need to enable JavaScript to run this app. antd 3.x版本中如何在React函数组件内使用getFieldDecorator?
优惠活动
大模型
产品
解决方案
定价
更多

antd 3.x版本中如何在React函数组件内使用getFieldDecorator?

问题原因&解决方案

你代码里的问题主要出在两个地方:

  • 组件入参解构错误:你写的const inviteModal = ( {props}) => 是错误的解构写法,函数组件的入参本身就是完整的props对象,你这种写法是尝试从props里提取名为props的属性,自然拿不到值,最终返回undefined。
  • 对Form.create()的注入逻辑理解错误:getFieldDecorator是Form.create()包装组件后,自动注入到组件props中的方法,既不是全局变量,也不需要作为参数传入Form.create()的配置项。

修正后的可运行代码

const FormItem = Form.Item

// 直接从props中解构取出Form.create注入的getFieldDecorator
const inviteModal = ({ getFieldDecorator }) => {
  return(
    <Form layout="vertical">
      <FormItem>
        {getFieldDecorator('email', {
          rules: [
            {
              required: true,
              message: i18n.t('email'),
            },
          ],
        })(<Input type="text" placeholder="email" />)}
      </FormItem> 
    </Form>
  )
}

// 不需要给Form.create传任何参数,直接包装组件即可
export default Form.create()(inviteModal)

补充说明

如果需要使用其他表单相关方法,比如validateFields、getFieldsValue等,都可以按照同样的方式从props中解构取出使用,不需要改成类组件即可实现所有antd 3.x Form的功能。

内容的提问来源于stack exchange,提问作者JGarnie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 16:45:11