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

342 个评论

  1. Hi from Order-Fulfillment.net

    Who would I speak with at your company about any of your order fulfillment and drop shipping needs?

    My company Order-Fulfillment.net can warehouse, inventory, and manage your drop shipping / order fulfillment for your customer orders.

    Based in the USA for almost 2 decades, near major USA air and sea ports.

    Here are some of the items we currently pick, pack and ship for clients:

    -Books, training manuals, guides
    -New member welcomes boxes and gifts
    -Product samples
    -Marketing materials
    -Medical program test kits
    -Follow up gifts to clients, leads, and prospects

    Thank you!

    Fulfillment Warehouse
    Order-Fulfillment.net
    Drop Shipping and Order Fulfillment Specialists since 2003

    Mascorro
  2. Right here is the right website for anybody
    who wants to understand this topic. You know a whole
    lot its almost hard to argue with you (not that I really will need to…HaHa).
    You definitely put a fresh spin on a topic that’s been discussed for a long time.

    Wonderful stuff, just excellent!

  3. Hi there! I know this is somewhat off-topic but I needed to ask.

    Does building a well-established blog like yours take a massive amount work?

    I am completely new to operating a blog however I do
    write in my journal everyday. I’d like to start a
    blog so I can share my experience and thoughts online.
    Please let me know if you have any kind of ideas or
    tips for brand new aspiring blog owners. Thankyou!

  4. Hello,

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

    We have made all our databases for sale for a once-off price.

    Visit our website to get the best bargain of your life. BestLocalData.com

    Regards,
    Gene

    Harkness
  5. Hello,

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

    We have made all our databases for sale for a once-off price.

    Visit our website to get the best bargain of your life. BestLocalData.com

    Regards,
    Renate

    Clausen
  6. Are you looking for the best way to market your business?

    FbCourses.net has the answer..

    Not sure where to start, or what to do?

    Why not learn from the best. All of the best.

    We are offering at never before seen prices all of the top courses for one insanely low price.

    If you have been thinking of how to generate more leads, website traffic or sales today is the day.

    All courses are available in full with immediate download on FbCourses.net

    Not sure which one you like, for the next 24 hours we are offering the entire suite of courses for the lowest amount you could possible imagine.

    Check us out : FbCourses.net

    You could be driving new income or starting the new side hustle today!

    Libby

发表评论

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