子组件:
// jshint esversion:6 import React, { Component } from 'react'; import { Form, Input} from 'antd'; const FormItem = Form.Item; class Forms extends Component{ getItemsValue = ()=>{ //3、自定义方法,用来传递数据(需要在父组件中调用获取数据) const values= this.props.form.getFieldsValue(); //4、getFieldsValue:获取一组输入控件的值,如不传入参数,则获取全部组件的值 return values; } render(){ const { form } = this.props; const { getFieldDecorator } = form; //1、将getFieldDecorator 解构出来,用于和表单进行双向绑定 return() } } export default Form.create()(Forms); //创建form实例
父组件:
import React, { Component } from 'react'; import { Modal } from 'antd'; import Forms from './Forms' export default class Modals extends Component { handleCancel = () => { this.props.closeModal(false); } handleCreate = () => { console.log(this.formRef.getItemsValue()); //6、调用子组件的自定义方法getItemsValue。注意:通过 this.formRef 才能拿到数据 this.props.getFormRef(this.formRef.getItemsValue()); this.props.closeModal(false); } render() { const { visible } = this.props; return (); } } this.formRef = form} //5、使用wrappedComponentRef 拿到子组件传递过来的ref(官方写法) />