文章
问答
冒泡
react父组件调用子组件的函数
//父组件
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} />
));
react-ref

关于作者

小乙哥
学海无涯,回头是岸
获得点赞
文章被阅读