react父组件调用子组件方法异常,代码如下
父组件部分代码:
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { Button, Input, Icon, Table, Pagination, Modal, Form, } from 'antd';
class UserTable extends Component {
onRowClick = (record, index) => {
console.log(this.refs.view);
this.refs.view.viewShowModal(record);
}
render() {
const { selectedRowKeys } = this.state;
const rowSelection = {
type: "radio",
selectedRowKeys,
onChange: this.onSelectChange,
onSelect: this.onSelect,
};
return (
<div>
<div style={{ marginBottom: 16 }}>
<UserModal style={{display:"inline-block"}} ref="view" />
</div>
<Table rowKey="id" pagination= {false} rowSelection={rowSelection} columns={columns} dataSource={this.state.datasource}
onRowClick={this.onRowClick} style={{minWidth:'700px',scroll:'overflow'}} />
</div>
);
}
}
export default UserTable;
子组件部分代码:
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { Layout,Button, Input, Modal, Form, DatePicker, Radio, Row, Col } from 'antd';
import moment from 'moment';
class UserModal extends React.Component {
state = { visible: false, user: {} }
viewShowModal = (recordData) => {
this.setState({visible: true, user: recordData});
}
render() {
const { getFieldDecorator } = this.props.form;
return (
<div>
test
</div>
);
}
}
UserModal = Form.create()(UserModal);
export default UserModal;
其中子组件必须用 Form.create(),而这样原本使用 ref 来获取子组件的函数就变得无法调用到,我想知道使用 Form.create() 后怎样才能从父组件中调用到子组件的函数
没有用过
antd
,不清楚antd
有没有提供获取ref
的方法,建议你可以先看antd - Form
的源码。如果找不到的话,给你提供另外一个思路,我猜你是想要调用
viewShowModal
这个函数: