父组件
import React, { useRef } from 'react';
import MyModl from './MyModl';
const Editor = () => {
let ChildRef:any = useRef();
const sumbitArticle = () => {
ChildRef.current.formResetFields();
}
return (
<div>
<MyModl onRef={ChildRef} />
</div>
);
}
export default Editor;
子组件
import React, {useImperativeHandle } from 'react';
const MyModel = (props: {onRef: any}) => {
useImperativeHandle(props.onRef, () => {
return {
formResetFields: formResetFields,
};
});
const formResetFields = () => {
console.log('我是被调用的方法')
}
return (
<Modal title="填写文章标题" visible={props.isModalVisible} ></Modal>
);
};
export default MyModel;
|