mirror of
https://github.com/streamich/react-use.git
synced 2026-01-25 14:17:16 +00:00
- it has more obvious name; - returns a function that check mount state -> more handy ti use due to less to write; - has tests;
16 lines
305 B
TypeScript
16 lines
305 B
TypeScript
import { useEffect, useRef } from 'react';
|
|
|
|
export default function useMountedState(): () => boolean {
|
|
const mountedRef = useRef<boolean>(false);
|
|
|
|
useEffect(() => {
|
|
mountedRef.current = true;
|
|
|
|
return () => {
|
|
mountedRef.current = false;
|
|
};
|
|
});
|
|
|
|
return () => mountedRef.current;
|
|
}
|