@os-design/use-forwarded-state 1.0.30 → 1.0.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,cAAc,EAMpB,MAAM,OAAO,CAAC;AAEf,UAAU,sBAAsB,CAAC,CAAC;IAChC,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,YAAY,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7B,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;CAC/B;AAMD,QAAA,MAAM,iBAAiB,GAAI,CAAC,sCAIzB,sBAAsB,CAAC,CAAC,CAAC,KAAG,CAC7B,CAAC,GAAG,SAAS,EACb,QAAQ,CAAC,cAAc,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CA4BxC,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,cAAc,EAMpB,MAAM,OAAO,CAAC;AAEf,UAAU,sBAAsB,CAAC,CAAC;IAChC,KAAK,CAAC,EAAE,CAAC,CAAC;IACV,YAAY,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7B,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;CAC/B;AAMD,QAAA,MAAM,iBAAiB,GAAI,CAAC,EAAE,oCAI3B,sBAAsB,CAAC,CAAC,CAAC,KAAG,CAC7B,CAAC,GAAG,SAAS,EACb,QAAQ,CAAC,cAAc,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAsCxC,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
package/dist/index.js CHANGED
@@ -6,17 +6,29 @@ const useForwardedState = ({
6
6
  onChange
7
7
  }) => {
8
8
  const [innerValue, setInnerValue] = useState(defaultValue);
9
+ const shouldUseInnerValue = useMemo(() => value === undefined, [value]);
10
+ const forwardedValue = useMemo(() => shouldUseInnerValue ? innerValue : value, [shouldUseInnerValue, innerValue, value]);
9
11
  const onChangeRef = useRef(onChange);
10
12
  useEffect(() => {
11
13
  onChangeRef.current = onChange;
12
14
  }, [onChange]);
13
- const shouldUseInnerValue = useMemo(() => value === undefined, [value]);
14
- const forwardedValue = useMemo(() => shouldUseInnerValue ? innerValue : value, [shouldUseInnerValue, innerValue, value]);
15
+ const forwardedValueRef = useRef(forwardedValue);
16
+ useEffect(() => {
17
+ forwardedValueRef.current = forwardedValue;
18
+ }, [forwardedValue]);
19
+ const shouldUseInnerValueRef = useRef(shouldUseInnerValue);
20
+ useEffect(() => {
21
+ shouldUseInnerValueRef.current = shouldUseInnerValue;
22
+ }, [shouldUseInnerValue]);
15
23
  const setForwardedValue = useCallback(v => {
16
- const nextValue = isFunction(v) ? v(forwardedValue) : v;
17
- if (shouldUseInnerValue) setInnerValue(nextValue);
18
- if (onChangeRef.current && nextValue !== undefined) onChangeRef.current(nextValue);
19
- }, [forwardedValue, shouldUseInnerValue]);
24
+ const nextValue = isFunction(v) ? v(forwardedValueRef.current) : v;
25
+ if (shouldUseInnerValueRef.current) {
26
+ setInnerValue(nextValue);
27
+ }
28
+ if (onChangeRef.current && nextValue !== undefined) {
29
+ onChangeRef.current(nextValue);
30
+ }
31
+ }, []);
20
32
  return [forwardedValue, setForwardedValue];
21
33
  };
22
34
  export default useForwardedState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@os-design/use-forwarded-state",
3
- "version": "1.0.30",
3
+ "version": "1.0.32",
4
4
  "license": "UNLICENSED",
5
5
  "repository": "git@gitlab.com:os-team/libs/os-design.git",
6
6
  "type": "module",
@@ -31,5 +31,5 @@
31
31
  "peerDependencies": {
32
32
  "react": "18"
33
33
  },
34
- "gitHead": "4d19733d7b0e81efcf2904c1820abc718155e920"
34
+ "gitHead": "60ba6c5b1658a4a302a3542e71e2ab3ce6407665"
35
35
  }
package/src/index.ts CHANGED
@@ -27,11 +27,6 @@ const useForwardedState = <T>({
27
27
  Dispatch<SetStateAction<T | undefined>>,
28
28
  ] => {
29
29
  const [innerValue, setInnerValue] = useState(defaultValue);
30
- const onChangeRef = useRef(onChange);
31
-
32
- useEffect(() => {
33
- onChangeRef.current = onChange;
34
- }, [onChange]);
35
30
 
36
31
  const shouldUseInnerValue = useMemo(() => value === undefined, [value]);
37
32
  const forwardedValue = useMemo(
@@ -39,17 +34,32 @@ const useForwardedState = <T>({
39
34
  [shouldUseInnerValue, innerValue, value]
40
35
  );
41
36
 
37
+ const onChangeRef = useRef(onChange);
38
+ useEffect(() => {
39
+ onChangeRef.current = onChange;
40
+ }, [onChange]);
41
+
42
+ const forwardedValueRef = useRef(forwardedValue);
43
+ useEffect(() => {
44
+ forwardedValueRef.current = forwardedValue;
45
+ }, [forwardedValue]);
46
+
47
+ const shouldUseInnerValueRef = useRef(shouldUseInnerValue);
48
+ useEffect(() => {
49
+ shouldUseInnerValueRef.current = shouldUseInnerValue;
50
+ }, [shouldUseInnerValue]);
51
+
42
52
  const setForwardedValue = useCallback<
43
53
  Dispatch<SetStateAction<T | undefined>>
44
- >(
45
- (v) => {
46
- const nextValue = isFunction(v) ? v(forwardedValue) : v;
47
- if (shouldUseInnerValue) setInnerValue(nextValue);
48
- if (onChangeRef.current && nextValue !== undefined)
49
- onChangeRef.current(nextValue);
50
- },
51
- [forwardedValue, shouldUseInnerValue]
52
- );
54
+ >((v) => {
55
+ const nextValue = isFunction(v) ? v(forwardedValueRef.current) : v;
56
+ if (shouldUseInnerValueRef.current) {
57
+ setInnerValue(nextValue);
58
+ }
59
+ if (onChangeRef.current && nextValue !== undefined) {
60
+ onChangeRef.current(nextValue);
61
+ }
62
+ }, []);
53
63
 
54
64
  return [forwardedValue, setForwardedValue];
55
65
  };