packageDocumentation module import { useEffect , useMemo , useRef } from "react" ; import { IDisposable } from "@itwin/core-bentley" ; public export function useDisposable < TDisposable extends IDisposable > ( createDisposable ) { return useOptionalDisposable ( createDisposable ) ! ; } public export function useOptionalDisposable < TDisposable extends IDisposable > ( createDisposable ) { const previous = useRef < TDisposable > ( ) ; const value = useMemo ( ( ) => { if ( previous . current ) previous . current . dispose ( ) ; previous . current = createDisposable ( ) ; return previous . current ; } , [ createDisposable ] ) ; useEffect ( ( ) => ( ) => previous . current && previous . current . dispose ( ) , [ ] ) ; return value ; }