//父组件
import React from 'react'
interface RefType {
handleValidateAll: () => boolean;
}
const Parents = () => {
const const childRef = React.useRef<RefType>();
const save = () => {
childRef.current.handleValidateAll()
}
return (
<Child ref={childRef} />
)
}
//子组件
import React from 'react';
const Child = (props: {eRef: React.
ForwardedRef<unknown>
}) => {
React.useImperativeHandle(props.eRef, () => {
return { handleValidateAll };
});
const handleValidateAll = () => {
console.log('父组件需要调用的子组件方法');
return !!(a && b)
}
return (
<div>Hello React</div>
)
}
export default React.forwardRef((props, ref) => (
<Child {...props} eRef={ref} />
));