ant D源码中form组件只对是react-component form的一层封装

首先表单是双向绑定的

双向绑定,廖雪峰老师是这样解释的

form在react中是怎么实现数据的双向绑定的?

在react中我们可以手动去绑定input框的值为一个state中的值,然后每次onchange时去更新state

刚看到createBaseForm.js的源码时,我猜测,reactForm也是这样实现的

默认的触发器就是onchange,再来看实现流程是怎么样的

createBaseForm返回一个高阶函数decorate

这里采用es5的类写法,getInitialState相当于Es6 Class中的constructor部分,在生成一个表单实例时就会有一个fieldsStore对象,通读全文后发现,很多复杂操都是在对这个全局store进行维护

getFieldDecorator是一个高阶函数,将传入的组件封装后返回

  • 核心部分onChange的封装

在onCollect部分先通过onCollectCommon实现了原action也就是onChange的执行,再setFields更新当前表单的值

更新store后,强制刷新

这里已经能够大概知道form内部是怎么运作的了,我画了一张流程图

根据原理做了一个简单的demo

  • npm

https://www.npmjs.com/package/zjx-react-form

  • 码云

https://gitee.com/onezilc/my-react-form.git

368 个评论

  1. Hello

    It is with sad regret to inform you that PCXLeads.com is shutting down

    We have made all our databases available to the public.

    Search your 1zilc.top, industry, phone numbers, emails, people..

    25 Million Companies globally

    500 Million Professionals

    143 Countries Included

    Get all this in our shutting down special for $149

    Regards,
    PCXLeads.com

    Walker Kerns
  2. Hello,

    It is with sad regret that after 12 years, LeadsMax.biz is shutting down.

    We have made all our databases available on our website.

    25 Million companies
    527 Million People

    LeadsMax.biz

    Callum Grier
  3. Hi,

    Your website is only listed in 8 out of a possible 12,489 directories.

    We are a service that lists your website in all these directories.

    Please visit us on DirectoryBump.com to find our more.

    Regards,
    Jann Bladin

    Jann Bladin
  4. Hello,

    I regret to inform you that LeadsTree.org will shut down Friday.

    We have now made all our databases available to the public on our website at a one-time fee.

    Visit us at LeadsTree.org

    Dale Carpentier
  5. Hello, did you know that there are 241,120 internet directories in the world.

    These websites are what drive traffic to YOUR business.

    Want more traffic? Want more Sales? We can help – today.

    Your website 1zilc.top is listed in only 7 of these directories.

    Get more traffic for your Global audience.

    Our automated system adds your website to all of the directories.

    You can find it here: getlisted.directory/1zilc.top

    Act today, and we will expedite your listings and waive the processing charge!

    We have a special going on. Use “FRIENDS” on checkout for a 50% discount valid today.

    Cathy Brackman
  6. Convert 1zilc.top to an app with one click!

    80% of users browse websites from their phones.

    Have the App send out push notifications without any extra marketing costs!

    MakeMySiteMobile.com

    Cecila Priestley
  7. Hello, did you know that there are 241,120 internet directories in the world.

    These websites are what drive traffic to YOUR business.

    Want more traffic? Want more Sales? We can help – today.

    Your website 1zilc.top is listed in only 7 of these directories.

    Get more traffic for your Global audience.

    Our automated system adds your website to all of the directories.

    You can find it here: getlisted.directory/1zilc.top

    Act today, and we will expedite your listings and waive the processing charge!

    Quinton Flanery
  8. Hello.

    We are offering Bullet Proof SMTP servers that never get suspended. Email as much as you want.

    DMCA ignored, bulletproof locations, 100% uptime guaranteed, unlimited data transfer, and 24/7/365 support.

    100 Spots available. BulletProofSMTP.org

    Angeles Dame
  9. It is with sad regret to inform you that BusinessLeads101.com is shutting down.

    We have over 300 million records available for a one time fee of $149.

    Please visit our website BusinessLeads101.com

    Regards,
    Eden

    Eden Tipton

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注