UNPKG

@undermuz/use-form

Version:
26 lines (25 loc) 766 B
// src/useForm/events/useFormOnError.ts import { useEffect, useRef } from "react"; import { isEqual } from "underscore"; import { useRefBy } from "../../utils/common.js"; var useFormOnError = (form, props) => { const { onError, options } = props; const onErrorRef = useRefBy(onError); const errorsRef = useRef(form.errors); useEffect(() => { var _a; if (!isEqual(errorsRef.current, form.errors)) { if (options == null ? void 0 : options.debug) console.log( "[useForm][On: Error]", errorsRef.current, form.errors ); errorsRef.current = form.errors; (_a = onErrorRef.current) == null ? void 0 : _a.call(onErrorRef, form.errors); } }, [form.errors]); }; export { useFormOnError };