'use strict';

var React = require('react');

/**
 * Returns the first React Child that receives `true` from isCorrectComponent
 * @param children React Children
 * @param isCorrectComponent a function that accepts a ReactElement and returns a boolean.
 * @returns The first child component that returns true on isCorrectComponent
 */
function useChildComponent(children, isCorrectComponent) {
    return React.useMemo(() => {
        return React.Children.toArray(children).find(d => React.isValidElement(d) && isCorrectComponent(d));
    }, [children]);
}

exports.useChildComponent = useChildComponent;
