import {
useImperativeHandle,
forwardRef,
} from 'react'
const Component = (props: any, ref: any) => {
useImperativeHandle(ref, () => ({
func1: handleChangeName ,
}))
const handleChangeName = async () => {
console.log('name')
}
}
export default forwardRef(Component)
import { FC , useRef } from 'react'
const FatherComponent : FC =()=>{
const component = useRef()
useEffect(() => {
// component 代表子组建
// func1 子组建的函数 实际调用handleChangeName
component.current.func1()
//console.log('name')
}, [])
return (
<div>
<Component ref={component} />
</div>
)
}