@trackunit/react-core-hooks 1.6.51 → 1.6.53

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.
Files changed (3) hide show
  1. package/index.cjs.js +22 -13
  2. package/index.esm.js +22 -13
  3. package/package.json +5 -5
package/index.cjs.js CHANGED
@@ -404,11 +404,11 @@ const usePrevious = (value) => {
404
404
  react.useEffect(() => {
405
405
  ref.current = value;
406
406
  }, [value]);
407
- const wrapper = {
407
+ const wrapper = react.useMemo(() => ({
408
408
  get previous() {
409
409
  return ref.current;
410
410
  },
411
- };
411
+ }), []);
412
412
  return wrapper.previous;
413
413
  };
414
414
 
@@ -513,25 +513,34 @@ const useLocalStorage = ({ key, defaultState, schema, onValidationFailed, onVali
513
513
  if (!key) {
514
514
  throw new Error("useLocalStorage key must be defined");
515
515
  }
516
- const [state, setState] = react.useState(initLocalStorageState({ key, defaultState, schema }));
516
+ const defaultStateRef = react.useRef(defaultState);
517
+ react.useEffect(() => {
518
+ defaultStateRef.current = defaultState;
519
+ }, [defaultState]);
520
+ const schemaRef = react.useRef(schema);
521
+ react.useEffect(() => {
522
+ schemaRef.current = schema;
523
+ }, [schema]);
524
+ const [state, setState] = react.useState(initLocalStorageState({ key, defaultState: defaultStateRef.current, schema: schemaRef.current }));
517
525
  const prevKey = usePrevious(key);
518
526
  react.useEffect(() => {
519
527
  if (key !== prevKey) {
520
- setState(initLocalStorageState({ key, defaultState, schema }));
528
+ setState(initLocalStorageState({ key, defaultState: defaultStateRef.current, schema: schemaRef.current }));
521
529
  }
522
- }, [key, prevKey, defaultState, schema]);
523
- useLocalStorageEffect({
530
+ }, [key, prevKey, schema]);
531
+ const localStorageProps = react.useMemo(() => ({
524
532
  key,
525
533
  state,
526
- defaultState,
527
- schema,
534
+ defaultState: defaultStateRef.current,
535
+ schema: schemaRef.current,
528
536
  onValidationFailed,
529
537
  onValidationSuccessful,
530
- });
531
- const reset = () => {
532
- setState(defaultState);
533
- };
534
- return [state, setState, reset];
538
+ }), [key, state, onValidationFailed, onValidationSuccessful]);
539
+ useLocalStorageEffect(localStorageProps);
540
+ const reset = react.useCallback(() => {
541
+ setState(defaultStateRef.current);
542
+ }, []);
543
+ return react.useMemo(() => [state, setState, reset], [state, setState, reset]);
535
544
  };
536
545
 
537
546
  /**
package/index.esm.js CHANGED
@@ -402,11 +402,11 @@ const usePrevious = (value) => {
402
402
  useEffect(() => {
403
403
  ref.current = value;
404
404
  }, [value]);
405
- const wrapper = {
405
+ const wrapper = useMemo(() => ({
406
406
  get previous() {
407
407
  return ref.current;
408
408
  },
409
- };
409
+ }), []);
410
410
  return wrapper.previous;
411
411
  };
412
412
 
@@ -511,25 +511,34 @@ const useLocalStorage = ({ key, defaultState, schema, onValidationFailed, onVali
511
511
  if (!key) {
512
512
  throw new Error("useLocalStorage key must be defined");
513
513
  }
514
- const [state, setState] = useState(initLocalStorageState({ key, defaultState, schema }));
514
+ const defaultStateRef = useRef(defaultState);
515
+ useEffect(() => {
516
+ defaultStateRef.current = defaultState;
517
+ }, [defaultState]);
518
+ const schemaRef = useRef(schema);
519
+ useEffect(() => {
520
+ schemaRef.current = schema;
521
+ }, [schema]);
522
+ const [state, setState] = useState(initLocalStorageState({ key, defaultState: defaultStateRef.current, schema: schemaRef.current }));
515
523
  const prevKey = usePrevious(key);
516
524
  useEffect(() => {
517
525
  if (key !== prevKey) {
518
- setState(initLocalStorageState({ key, defaultState, schema }));
526
+ setState(initLocalStorageState({ key, defaultState: defaultStateRef.current, schema: schemaRef.current }));
519
527
  }
520
- }, [key, prevKey, defaultState, schema]);
521
- useLocalStorageEffect({
528
+ }, [key, prevKey, schema]);
529
+ const localStorageProps = useMemo(() => ({
522
530
  key,
523
531
  state,
524
- defaultState,
525
- schema,
532
+ defaultState: defaultStateRef.current,
533
+ schema: schemaRef.current,
526
534
  onValidationFailed,
527
535
  onValidationSuccessful,
528
- });
529
- const reset = () => {
530
- setState(defaultState);
531
- };
532
- return [state, setState, reset];
536
+ }), [key, state, onValidationFailed, onValidationSuccessful]);
537
+ useLocalStorageEffect(localStorageProps);
538
+ const reset = useCallback(() => {
539
+ setState(defaultStateRef.current);
540
+ }, []);
541
+ return useMemo(() => [state, setState, reset], [state, setState, reset]);
533
542
  };
534
543
 
535
544
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-hooks",
3
- "version": "1.6.51",
3
+ "version": "1.6.53",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -10,11 +10,11 @@
10
10
  "react": "19.0.0",
11
11
  "jest-fetch-mock": "^3.0.3",
12
12
  "zod": "^3.23.8",
13
- "@trackunit/react-core-contexts-api": "1.7.49",
14
- "@trackunit/iris-app-runtime-core": "1.7.50",
15
- "@trackunit/shared-utils": "1.8.48",
13
+ "@trackunit/react-core-contexts-api": "1.7.51",
14
+ "@trackunit/iris-app-runtime-core": "1.7.52",
15
+ "@trackunit/shared-utils": "1.8.50",
16
16
  "@tanstack/react-router": "1.114.29",
17
- "@trackunit/react-test-setup": "1.3.48"
17
+ "@trackunit/react-test-setup": "1.3.50"
18
18
  },
19
19
  "module": "./index.esm.js",
20
20
  "main": "./index.cjs.js",