main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
826 bytes
|
Line | |
---|
1 | import { useCallback, useEffect, useRef, useState } from 'react';
|
---|
2 | export default function useStateValidator(state, validator, initialState) {
|
---|
3 | if (initialState === void 0) { initialState = [undefined]; }
|
---|
4 | var validatorInner = useRef(validator);
|
---|
5 | var stateInner = useRef(state);
|
---|
6 | validatorInner.current = validator;
|
---|
7 | stateInner.current = state;
|
---|
8 | var _a = useState(initialState), validity = _a[0], setValidity = _a[1];
|
---|
9 | var validate = useCallback(function () {
|
---|
10 | if (validatorInner.current.length >= 2) {
|
---|
11 | validatorInner.current(stateInner.current, setValidity);
|
---|
12 | }
|
---|
13 | else {
|
---|
14 | setValidity(validatorInner.current(stateInner.current));
|
---|
15 | }
|
---|
16 | }, [setValidity]);
|
---|
17 | useEffect(function () {
|
---|
18 | validate();
|
---|
19 | }, [state]);
|
---|
20 | return [validity, validate];
|
---|
21 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.