import { RefObject , useLayoutEffect , useState } from "react" ; import { isNil } from "./assertions" ; export function useHasChild ( querySelector , rootRef ) { const [ result , setResult ] = useState ( false ) ; useLayoutEffect ( ( ) => { if ( ! isNil ( rootRef . current ) ) { setResult ( ! isNil ( rootRef . current . querySelector ( `:scope > ${ querySelector }` ) ) ) ; } } ) ; return result ; } example export function useHasChildren ( querySelectors , rootRef ) { const [ queryResults , setResults ] = useState < Record < string , boolean > > ( { } ) ; useLayoutEffect ( ( ) => { const element = rootRef . current ; if ( ! isNil ( element ) ) { let isDirty = false ; const newResults = Object . keys ( querySelectors ) . reduce ( ( results , x ) => { const result = ! isNil ( element . querySelector ( querySelectors [ x ] ) ) ; isDirty = isDirty || queryResults [ x ] !== result ; results [ x ] = result ; return results ; } , { } ) ; if ( isDirty ) { setResults ( newResults ) ; } } } ) ; return queryResults ; }