25 lines
931 B
JavaScript
25 lines
931 B
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var react_1 = require("react");
|
|
function useStateValidator(state, validator, initialState) {
|
|
if (initialState === void 0) { initialState = [undefined]; }
|
|
var validatorInner = react_1.useRef(validator);
|
|
var stateInner = react_1.useRef(state);
|
|
validatorInner.current = validator;
|
|
stateInner.current = state;
|
|
var _a = react_1.useState(initialState), validity = _a[0], setValidity = _a[1];
|
|
var validate = react_1.useCallback(function () {
|
|
if (validatorInner.current.length >= 2) {
|
|
validatorInner.current(stateInner.current, setValidity);
|
|
}
|
|
else {
|
|
setValidity(validatorInner.current(stateInner.current));
|
|
}
|
|
}, [setValidity]);
|
|
react_1.useEffect(function () {
|
|
validate();
|
|
}, [state]);
|
|
return [validity, validate];
|
|
}
|
|
exports.default = useStateValidator;
|