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

357 个评论

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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

Angeles Dame进行回复 取消回复

您的电子邮箱地址不会被公开。