by Pawan Kumar @jsartisan
What will be the output in console for the following code:
function App() {
const [show, setShow] = useState(true)
return <div>
{show && <Child unmount={() => setShow(false)} />}
</div>;
}
function Child({ unmount }) {
const isMounted = useIsMounted()
useEffect(() => {
console.log(isMounted)
Promise.resolve(true).then(() => {
console.log(isMounted)
});
unmount();
}, []);
return null;
};
function useIsMounted() {
const isMounted = useRef(false);
useEffect(() => {
isMounted.current = true;
return () => isMounted.current = false;
}, []);
return isMounted.current;
}