美文网首页
React Hooks父组件中获取子组件Antd Form的fi

React Hooks父组件中获取子组件Antd Form的fi

作者: 一个被程序员耽误的厨师 | 来源:发表于2020-07-23 10:17 被阅读0次
    image.png
    index.js:1 Warning: you should not use `ref` on enhanced form, please use `wrappedComponentRef`. See: https://github.com/react-component/form#note-use-wrappedcomponentref-instead-of-withref-after-rc-form140
    console.<computed> @ index.js:1
    printWarning @ warning.js:34
    warning @ warning.js:57
    _this.<computed> @ createBaseForm.js:64
    handleSubmit @ index.js:262
    onOk @ index.js:39
    Modal._this.handleOk @ Modal.js:95
    Button._this.handleClick @ button.js:136
    callCallback @ react-dom.development.js:188
    invokeGuardedCallbackDev @ react-dom.development.js:237
    invokeGuardedCallback @ react-dom.development.js:292
    invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:306
    executeDispatch @ react-dom.development.js:389
    executeDispatchesInOrder @ react-dom.development.js:411
    executeDispatchesAndRelease @ react-dom.development.js:3278
    executeDispatchesAndReleaseTopLevel @ react-dom.development.js:3287
    forEachAccumulated @ react-dom.development.js:3259
    runEventsInBatch @ react-dom.development.js:3304
    runExtractedPluginEventsInBatch @ react-dom.development.js:3514
    handleTopLevel @ react-dom.development.js:3558
    batchedEventUpdates$1 @ react-dom.development.js:21871
    batchedEventUpdates @ react-dom.development.js:795
    dispatchEventForLegacyPluginEventSystem @ react-dom.development.js:3568
    attemptToDispatchEvent @ react-dom.development.js:4267
    dispatchEvent @ react-dom.development.js:4189
    unstable_runWithPriority @ scheduler.development.js:653
    runWithPriority$1 @ react-dom.development.js:11039
    discreteUpdates$1 @ react-dom.development.js:21887
    discreteUpdates @ react-dom.development.js:806
    dispatchDiscreteEvent @ react-dom.development.js:4168
    index.js:1 Warning: You cannot set a form field before rendering a field associated with the value.
    console.<computed> @ index.js:1
    printWarning @ warning.js:34
    warning @ warning.js:57
    treeTraverse @ utils.js:42
    (anonymous) @ utils.js:47
    treeTraverse @ utils.js:45
    flattenFields @ utils.js:54
    flattenRegisteredFields @ createFieldsStore.js:38
    setFieldsInitialValue @ createFieldsStore.js:248
    _this.<computed> @ createBaseForm.js:66
    handleSubmit @ index.js:262
    onOk @ index.js:39
    Modal._this.handleOk @ Modal.js:95
    Button._this.handleClick @ button.js:136
    callCallback @ react-dom.development.js:188
    invokeGuardedCallbackDev @ react-dom.development.js:237
    invokeGuardedCallback @ react-dom.development.js:292
    invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:306
    executeDispatch @ react-dom.development.js:389
    executeDispatchesInOrder @ react-dom.development.js:411
    executeDispatchesAndRelease @ react-dom.development.js:3278
    executeDispatchesAndReleaseTopLevel @ react-dom.development.js:3287
    forEachAccumulated @ react-dom.development.js:3259
    runEventsInBatch @ react-dom.development.js:3304
    runExtractedPluginEventsInBatch @ react-dom.development.js:3514
    handleTopLevel @ react-dom.development.js:3558
    batchedEventUpdates$1 @ react-dom.development.js:21871
    batchedEventUpdates @ react-dom.development.js:795
    dispatchEventForLegacyPluginEventSystem @ react-dom.development.js:3568
    attemptToDispatchEvent @ react-dom.development.js:4267
    dispatchEvent @ react-dom.development.js:4189
    unstable_runWithPriority @ scheduler.development.js:653
    runWithPriority$1 @ react-dom.development.js:11039
    discreteUpdates$1 @ react-dom.development.js:21887
    discreteUpdates @ react-dom.development.js:806
    dispatchDiscreteEvent @ react-dom.development.js:4168
    Show 6 more frames
    index.js:1 Warning: you should not use `ref` on enhanced form, please use `wrappedComponentRef`. See: https://github.com/react-component/form#note-use-wrappedcomponentref-instead-of-withref-after-rc-form140
    

    根据官网给出的实例,可以通过useImperativeHandle将子组件中props.form.getFieldsValue()暴露给父组件,从而在父组件中就可以拿到表单值,代码如下:(注:代码只是作为方法示例,不作为可运行实例)
    父组件

    import React, { 
      useRef
    } from 'react'
    
    import { Modal } from 'antd'
    import MyForm from './myForm'
    
    const ParentComp = () => {
    
        const getFormValue = useRef();
    
        const handleOk = () => {
          const fields = getFormValue.current.formFields;
          console.log(fields)
        }
    
        return <Modal onOk={handleOk} >
                 <MyForm
                   wrappedComponentRef={getFormValue}
                 />
               </Modal>
    }
    
    export default ParentComp 
    
    

    子组件(myForm.jsx)

    import React, { useImperativeHandle, useRef, forwardRef } from 'react'
    import { Form } from 'antd'
    
    const MyForm = (props, ref) => {
      const formRef = useRef()
    
      useImperativeHandle(ref, () => ({
        formFields: props.form.getFieldsValue()
      }))
    
      return <Form ref={formRef}>
         ...
      </Form>
    }
    
    const WrappedForm = Form.create({ name: 'form' })(forwardRef(MyForm))
    
    export default WrappedForm
    

    相关文章

      网友评论

          本文标题:React Hooks父组件中获取子组件Antd Form的fi

          本文链接:https://www.haomeiwen.com/subject/rpyclktx.html