MENU
Calling Child Component Methods
Insofar as calling child component methods is concerned, using useImperativeHandle is identical to adding methods to a class component:
RESETRUNFULL
class Comp extends React.Component {
print() {
console.log('comp');
}
render() {
return (<div>comp</div>)
}}
is identical to:
RESETRUNFULL
const Comp = forwardRef((props, ref) => {
useImperativeHandle(ref, () => ({
print: () => {
console.log('comp');
}
}), []);
return <div>comp</div>});
However, you should avoid calling methods of child components altogether as it is a bad practice.