Make useMountedState return a single function instead of a new one each time;

This commit is contained in:
xobotyi 2019-08-14 03:33:11 +03:00
parent 987e08af61
commit 12608fe5be

View File

@ -1,7 +1,8 @@
import { useEffect, useRef } from 'react';
import { useEffect, useRef, useCallback } from 'react';
export default function useMountedState(): () => boolean {
const mountedRef = useRef<boolean>(false);
const get = useCallback(() => mountedRef.current, []);
useEffect(() => {
mountedRef.current = true;
@ -11,5 +12,5 @@ export default function useMountedState(): () => boolean {
};
});
return () => mountedRef.current;
return get;
}