Skip to content

Commit

Permalink
fix: add isMounted in useWindowSize
Browse files Browse the repository at this point in the history
affects: @medly-components/utils
  • Loading branch information
gmukul01 committed Mar 17, 2024
1 parent b84b780 commit 68466d5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
12 changes: 3 additions & 9 deletions packages/utils/src/hooks/useAxios/useAxios.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios, { AxiosResponse } from 'axios';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useCallback, useState } from 'react';
import { useIsMounted } from '../useIsMounted';
import { Result } from './types';

/**
Expand All @@ -12,7 +13,7 @@ export const useAxios = <Data = any, Error = { message: string; variant: 'error'
[response, setResponse] = useState<AxiosResponse<Data>>(),
[error, setError] = useState<AxiosResponse<Error>>(),
[isLoading, setLoadingState] = useState(false),
isMounted = useRef<boolean>(true);
isMounted = useIsMounted();

const request = useCallback(async ({ onSuccess, onError, ...config }) => {
setLoadingState(true);
Expand Down Expand Up @@ -40,13 +41,6 @@ export const useAxios = <Data = any, Error = { message: string; variant: 'error'
});
}, []);

useEffect(
() => () => {
isMounted.current = false;
},
[]
);

const clear = useCallback(() => {
setData(undefined);
setResponse(undefined);
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/hooks/useIsMounted/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useIsMounted';
14 changes: 14 additions & 0 deletions packages/utils/src/hooks/useIsMounted/useIsMounted.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useEffect, useRef } from 'react';

export const useIsMounted = () => {
const isMounted = useRef<boolean>(true);

useEffect(
() => () => {
isMounted.current = false;
},
[]
);

return isMounted;
};
12 changes: 3 additions & 9 deletions packages/utils/src/hooks/useStorage/useStorage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useContext, useEffect, useRef, useState } from 'react';
import { useContext, useEffect, useState } from 'react';
import { COOKIE_STORAGE } from '../../helpers/cookieStorage';
import { LOCAL_STORAGE } from '../../helpers/localStorage';
import { SESSION_STORAGE } from '../../helpers/sessionStorage';
import { useIsMounted } from '../useIsMounted';
import { StorageConfig } from './Storage.context';
import { UseStorageOptions, UseStorageSetValue } from './types';

Expand All @@ -24,7 +25,7 @@ export const useStorage = <T>(key: string, currOptions?: UseStorageOptions<T>):
options = currOptions ?? contextOption,
storage = storageObj[options.storage || 'localStorage'],
{ initialValue } = options,
isMounted = useRef<boolean>(true);
isMounted = useIsMounted();

const readValue = (): T => {
if (typeof window === 'undefined') {
Expand Down Expand Up @@ -58,13 +59,6 @@ export const useStorage = <T>(key: string, currOptions?: UseStorageOptions<T>):
}
};

useEffect(
() => () => {
isMounted.current = false;
},
[]
);

useEffect(() => {
const handleStorageChange = () => isMounted.current && setStoredValue(readValue());

Expand Down
3 changes: 3 additions & 0 deletions packages/utils/src/hooks/useWindowSize/useWindowSize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useState } from 'react';
import { debounce } from '../../helpers';
import { useIsMounted } from '../useIsMounted';

/**
* A hook to get the window size
Expand All @@ -8,6 +9,7 @@ import { debounce } from '../../helpers';
*
*/
export const useWindowSize = () => {
const isMounted = useIsMounted();
const [windowSize, setWindowSize] = useState({
width: typeof window !== 'undefined' ? window.innerWidth : 0,
height: typeof window !== 'undefined' ? window.innerHeight : 0
Expand All @@ -16,6 +18,7 @@ export const useWindowSize = () => {
useEffect(() => {
const handleResize = debounce(
() =>
isMounted.current &&
setWindowSize({
width: window.innerWidth,
height: window.innerHeight
Expand Down

0 comments on commit 68466d5

Please sign in to comment.