UNPKG

@dark-engine/core

Version:

The lightweight and powerful UI rendering engine without dependencies and written in TypeScript (Browser, Node.js, Android, iOS, Windows, Linux, macOS)

33 lines (32 loc) 1.09 kB
import { __useCursor as useCursor } from '../internal'; import { detectIsFunction } from '../utils'; import { useUpdate } from '../use-update'; import { useEffect } from '../use-effect'; import { component } from '../component'; import { useState } from '../use-state'; import { useEvent } from '../use-event'; function useError() { const cursor = useCursor(); const update = useUpdate(); const [error, setError] = useState(null); const reset = useEvent(() => setError(null)); cursor.hook.setIsBoundary(true); cursor.hook.setCatch(setError); cursor.hook.setUpdate(update); return [error, reset]; } const ErrorBoundary = component( ({ fallback = null, renderFallback, onError, slot }) => { const [error, reset] = useError(); useEffect(() => { if (!error) return; detectIsFunction(onError) && onError(error); }, [error]); return error ? (detectIsFunction(renderFallback) ? renderFallback({ error, reset }) : fallback) : slot; }, { displayName: 'ErrorBoundary', }, ); export { useError, ErrorBoundary }; //# sourceMappingURL=boundary.js.map