import { useRef } from 'react'; import { TestContext, TestFunction } from 'yup'; function cacheTest( asyncValidate: TestFunction ): TestFunction { let valid = true; let value: T | undefined; return async (newValue: T, context: TestContext) => { if (newValue !== value) { value = newValue; const response = await asyncValidate.call(context, newValue, context); valid = !!response; } return valid; }; } export function useCachedValidation( test: TestFunction ) { const ref = useRef(cacheTest(test)); return ref.current; }