1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import React, { useState, useRef } from 'react'; import { Table, Card, Button, } from 'antd'; import ChildComp from './child';
const FComp: React.FC<any> = (props) => { let child = useRef();; let [text,setText] = useState<string>("我是父组件"); function onChild(){ console.log("subMitData", child.current.childMethod()); let childName = child.current.childMethod(); setText(childName); } return ( <ChildComp onRef={child} /> <Button type='primary' onClick={onChild}>调用子组件</Button> <h1>{text}<h1> ) }
|