Skip to content

Commit

Permalink
Initialize with mounted 'false'?
Browse files Browse the repository at this point in the history
IMHO when the hook runs for the first time, the component is not mounted already, so the initial value should be false.

Only after the (first) execution of useEffect it is mounted and isMounted should change to true.

(Makes probably not a big difference in real life, but feels more correct)
  • Loading branch information
nilshartmann committed Jul 18, 2019
1 parent 312a01c commit 751f0f5
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions index.js
@@ -1,9 +1,10 @@
import { useRef, useEffect } from 'react';

const useIsMounted = () => {
const isMounted = useRef(true);
useEffect(() => () => {
isMounted.current = false;
const isMounted = useRef(false);
useEffect(() => {
isMounted.current = true;
return () => isMounted.current = false;
}, []);
return isMounted;
};
Expand Down

0 comments on commit 751f0f5

Please sign in to comment.