useControl

export function useControl(CompIn) {
    const ref = useRef();
    // 通过 proxy 实现支持各种方法名
    const methods = useMemo(() => createMethodsProxy(ref), []);
    const CompOut = useMemo(() => {
        if (!CompIn) {
            return null;
        }
        const ControlledComp = function ControlledComp(props) {
            return <CompIn {...props} ref={ref} />;
        };
        if (process.env.NODE_ENV === 'development') {
            const originalName = CompIn.displayName ?? CompIn.name ?? 'Unknown';
            ControlledComp.displayName = `useControl(${originalName})`;
        }
        return ControlledComp;
    }, [CompIn]);
    return [CompOut, methods];
}

function createMethodsProxy(ref) {
    return new Proxy(
        {
            $get(property) {
                return ref.current?.[property];
            },
        },
        {
            get(target, property) {
                if (target[property]) {
                    return target[property];
                }
                const method = async (...args) => {
                    const fn = ref.current?.[property];
                    const value = fn && await fn(...args);
                    return value;
                };
                target[property] = method;
                return method;
            },
        }
    );
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容