import { useMemo } from 'react' ; import { BITS , useFormContext } from './Contexts' ; import memoize from 'memoize-one' ; import shallowequal from 'shallowequal' ; import { filterAndMapErrors , inclusiveMapErrors } from './Errors' ; import { Errors } from './types' ; type UseErrorOptions = { inclusive ? : boolean ; } ; function isFilterErrorsEqual ( [ a ] , [ b ] ) { let isEqual = ( a . errors === b . errors || shallowequal ( a . errors , b . errors ) ) && a . names === b . names && a . mapErrors === b . mapErrors ; return isEqual ; } param paths param options param { boolean = } options . inclusive returns { Errors } function useErrors ( paths ? , { inclusive } = { } ) { const errors = useFormContext ( BITS . errors ) . errors ; const memoFilterAndMapErrors = useMemo ( ( ) => memoize ( filterAndMapErrors , isFilterErrorsEqual ) , [ ] ) ; return paths ? memoFilterAndMapErrors ( { errors , names : paths , mapErrors : ! inclusive ? undefined : inclusiveMapErrors , } ) : errors ; } export default useErrors ;