The parent component
import Son from './son'; import { useRef } from 'react'; export default () => { const childRef = useRef(); Const updateChildState = () = > {/ / changeVal is the way children exposed to the parent component childRef. Current. ChangeVal (99); }; Return (<div> fuqin <Son ref={childRef} /> <button onClick={updateChildState}> trigger child component method </button> </div>); };Copy the code
Child components
import { useState, useImperativeHandle, forwardRef } from 'react'; Export default forwardRef((props, ref) => {useImperativeHandle(ref, () => ({useImperativeHandle(ref, ()) => ( (newVal) => { console.log(123, newVal); }})); return <div>son</div>; });Copy the code