@trackunit/react-core-hooks 1.3.33 → 1.3.35

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 +21 -15
  2. package/index.esm.js +21 -15
  3. package/package.json +4 -4
package/index.cjs.js CHANGED
@@ -865,6 +865,21 @@ const useCurrentUser = () => {
865
865
  return context;
866
866
  };
867
867
 
868
+ /**
869
+ * The usePrevious hook is a useful tool for tracking the previous value of a variable in a functional component. This can be particularly handy in scenarios where it is necessary to compare the current value with the previous one, such as triggering actions or rendering based on changes.
870
+ *
871
+ * @example
872
+ * const [color, setColor] = React.useState(getRandomColor());
873
+ const previousColor = usePrevious(color);
874
+ */
875
+ const usePrevious = (value) => {
876
+ const ref = react.useRef(undefined);
877
+ react.useEffect(() => {
878
+ ref.current = value;
879
+ }, [value]);
880
+ return ref.current;
881
+ };
882
+
868
883
  /**
869
884
  * Validates the state object using a Zod schema.
870
885
  *
@@ -904,21 +919,6 @@ const initLocalStorageState = ({ key, defaultState, schema, }) => {
904
919
  return validateState({ state: localStorageItemJSON, defaultState, schema });
905
920
  };
906
921
 
907
- /**
908
- * The usePrevious hook is a useful tool for tracking the previous value of a variable in a functional component. This can be particularly handy in scenarios where it is necessary to compare the current value with the previous one, such as triggering actions or rendering based on changes.
909
- *
910
- * @example
911
- * const [color, setColor] = React.useState(getRandomColor());
912
- const previousColor = usePrevious(color);
913
- */
914
- const usePrevious = (value) => {
915
- const ref = react.useRef(undefined);
916
- react.useEffect(() => {
917
- ref.current = value;
918
- }, [value]);
919
- return ref.current;
920
- };
921
-
922
922
  /**
923
923
  * Sets a value in the local storage with the specified key.
924
924
  * Thin wrapper around localStorage.setItem() that is slightly more type safe
@@ -982,6 +982,12 @@ const useLocalStorage = ({ key, defaultState, schema, onValidationFailed, onVali
982
982
  throw new Error("useLocalStorage key may not be falsy");
983
983
  }
984
984
  const [state, setState] = react.useState(initLocalStorageState({ key, defaultState, schema }));
985
+ const prevKey = usePrevious(key);
986
+ react.useEffect(() => {
987
+ if (key !== prevKey) {
988
+ setState(initLocalStorageState({ key, defaultState, schema }));
989
+ }
990
+ }, [key, prevKey, defaultState, schema]);
985
991
  useLocalStorageEffect({
986
992
  key,
987
993
  state,
package/index.esm.js CHANGED
@@ -863,6 +863,21 @@ const useCurrentUser = () => {
863
863
  return context;
864
864
  };
865
865
 
866
+ /**
867
+ * The usePrevious hook is a useful tool for tracking the previous value of a variable in a functional component. This can be particularly handy in scenarios where it is necessary to compare the current value with the previous one, such as triggering actions or rendering based on changes.
868
+ *
869
+ * @example
870
+ * const [color, setColor] = React.useState(getRandomColor());
871
+ const previousColor = usePrevious(color);
872
+ */
873
+ const usePrevious = (value) => {
874
+ const ref = useRef(undefined);
875
+ useEffect(() => {
876
+ ref.current = value;
877
+ }, [value]);
878
+ return ref.current;
879
+ };
880
+
866
881
  /**
867
882
  * Validates the state object using a Zod schema.
868
883
  *
@@ -902,21 +917,6 @@ const initLocalStorageState = ({ key, defaultState, schema, }) => {
902
917
  return validateState({ state: localStorageItemJSON, defaultState, schema });
903
918
  };
904
919
 
905
- /**
906
- * The usePrevious hook is a useful tool for tracking the previous value of a variable in a functional component. This can be particularly handy in scenarios where it is necessary to compare the current value with the previous one, such as triggering actions or rendering based on changes.
907
- *
908
- * @example
909
- * const [color, setColor] = React.useState(getRandomColor());
910
- const previousColor = usePrevious(color);
911
- */
912
- const usePrevious = (value) => {
913
- const ref = useRef(undefined);
914
- useEffect(() => {
915
- ref.current = value;
916
- }, [value]);
917
- return ref.current;
918
- };
919
-
920
920
  /**
921
921
  * Sets a value in the local storage with the specified key.
922
922
  * Thin wrapper around localStorage.setItem() that is slightly more type safe
@@ -980,6 +980,12 @@ const useLocalStorage = ({ key, defaultState, schema, onValidationFailed, onVali
980
980
  throw new Error("useLocalStorage key may not be falsy");
981
981
  }
982
982
  const [state, setState] = useState(initLocalStorageState({ key, defaultState, schema }));
983
+ const prevKey = usePrevious(key);
984
+ useEffect(() => {
985
+ if (key !== prevKey) {
986
+ setState(initLocalStorageState({ key, defaultState, schema }));
987
+ }
988
+ }, [key, prevKey, defaultState, schema]);
983
989
  useLocalStorageEffect({
984
990
  key,
985
991
  state,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-hooks",
3
- "version": "1.3.33",
3
+ "version": "1.3.35",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -10,9 +10,9 @@
10
10
  "react": "19.0.0",
11
11
  "jest-fetch-mock": "^3.0.3",
12
12
  "zod": "3.22.4",
13
- "@trackunit/react-core-contexts-api": "1.4.33",
14
- "@trackunit/iris-app-runtime-core": "1.4.33",
15
- "@trackunit/shared-utils": "1.5.32"
13
+ "@trackunit/react-core-contexts-api": "1.4.35",
14
+ "@trackunit/iris-app-runtime-core": "1.4.35",
15
+ "@trackunit/shared-utils": "1.5.34"
16
16
  },
17
17
  "module": "./index.esm.js",
18
18
  "main": "./index.cjs.js",