score:-1
make sure that you are not creating a new instance of the debouncer each time:
import { debounce } from 'lodash'
const isjimmy = async (value,resolve) => {
const response = await fetch('/is-jimmy/' + value))
resolve(response.responsetext === 'true')
}
const debounceinstance = (func,delay=1000) => {
const funcdebounced = debounce(func,delay)
return (newvalue) =>
new promise(resolve =>
funcdebounced(newvalue,resolve))
}
//bad - creates a new instance on each call
const asyncjimmyschema = string()
.test(val=> debounceinstance(isjimmy)(val)});
//good - same instance for each call
const asyncjimmyschema = string()
.test(debounceinstance(isjimmy))}
score:1
i think this should work. it's copying the solution from just-debounce-it but returning a promise
right away, which is what yup expects.
const asyncdebouncer = (fn, wait, callfirst) => {
var timeout;
return function() {
return new promise(async (resolve) => {
if (!wait) {
const result = await fn.apply(this, arguments);
resolve(result);
}
var context = this;
var args = arguments;
var callnow = callfirst && !timeout;
cleartimeout(timeout);
timeout = settimeout(async function() {
timeout = null;
if (!callnow) {
const result = await fn.apply(context, args);
resolve(result);
}
}, wait);
if (callnow) {
const result = await fn.apply(this, arguments);
resolve(result);
}
});
};
};
let asyncjimmyschema = string().test(
'is-jimmy',
'${path} is not jimmy',
asyncdebouncer((value) => (await fetch('/is-jimmy/' + value)).responsetext === 'true', 400),
});
score:5
you can implement it by your self by using lodash.debounce
:
import { debounce } from "lodash";
// .....
const async_validation_timeout_in_ms = 1000;
const validationfunction = async (value, resolve) => {
try {
const response = await fetch('/is-jimmy/' + value);
resolve(response.responsetext === 'true');
} catch (error) {
resolve(false);
}
};
const validationdebounced = debounce(validationfunction, async_validation_timeout_in_ms);
then in the validation scheme:
let asyncjimmyschema = string().test(
'is-jimmy',
'${path} is not jimmy',
value => new promise(resolve => validationdebounced(value, resolve)),
});
Source: stackoverflow.com
Related Query
- Yup / Formik async validation with debounce
- Formik async field validation with yup schema
- Async validation with Formik, Yup and React
- Formik & yup form validation not working as expected with VirtualizedSelect
- Validation using Formik with Yup and React-select
- React + Formik + Yup async validation
- yup form validation with formik using regex not working as expected with bangla font input field
- Yup conditional validation with Formik
- Validation 2 fields with yup and formik
- How to get the Yup conditional validation to work with Formik in Reactjs?
- Value from Selector doesn't work with Formik and Yup validation
- Yup validation with regex using matches problem
- Formik, Yup Password Strength Validation with React
- Automatically trim white spaces with Yup and Formik
- How to use React with Yup Validation (Without Formik)
- Formik React with 2 buttons (Submit and Save) to submit form - Save button not to trigger validation
- Formik and yup validation on string type using latest release 0.29.3
- Yup validation with FieldsArray, ReactJS, redux-from
- Object or String type conditional with yup validation
- Edit an entry with Formik and Yup doesn't recognize the existing fields
- Yup validation for loginID with dynamic data type(either email or phone number)
- redirect in formik with yup react?
- How to show Form error summary using Formik with yup validationSchema
- Yup / Formik validation using dynamic keys
- Yup /w Formik File Upload Validation
- Yup Validation With Multiple Condition based on other field value
- yup validation to Validate formki field whether starts with value of other input field
- Yup validation for nested form elements in formik
- Radio button validation not disappear when select a button using Formik and Yup
- Validation works incorrectly for React-select with Formik on mobile device
More Query from same tag
- In react form, cannot type in text fields or click or hover over button
- How to use hover not included in the react code?
- Make tailwindcss navbar component close on scroll
- Checking value in array by map function and also displaying alert box just once
- React Router 3 not rendering correct component
- Set state of react component by using redux store
- how to show a hidden element on hover on tailwind
- TypeError: readonly assignment error with React application in Safari
- Converting Functional Component code to Class Component code
- I have to click the display button twice to render the data to the screen?
- How do I display/render image from mongoDB with reactjs?
- props works incorrectly with componentDidUpdate.(Cash pollution)
- Can not change the color of canvas bars with audio progress
- React screen size if condition returns undefined
- React testing with Jest : Destructuring assignment not working
- Determing if a value in an array is in the past, present or future
- How to import a Widget designed for JQuery in my React projet?
- Map response from GitHub API to React.js with Typescript
- How to use MgtTeamsChannelPicker in React?
- OpenLayer 4 draw arrows on map
- firebase__webpack_imported_module_2__.auth.createuserwithemailandpassword is not a function.?
- How to make requests in react correctly?
- Assigning an error message in Formik validation in TypeScript not working
- Render a component when another component is clicked
- How to prevent re-rendering with callbacks as props in ReactJS?
- Functions passed to react component children don't receive function context
- concat two json objects from different api by id in axios get and store
- React & Reselect selector claims state is the same after update
- how to select a styled-component and access its property (ex: offsetHeight)
- React state is null inside of return