This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function useAsync(asyncFn, onSuccess) { | |
useEffect(() => { | |
let isActive = true; | |
asyncFn().then(data => { | |
if (isActive) onSuccess(data); | |
}); | |
return () => { isActive = false }; | |
}, [asyncFn, onSuccess]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
useEffect(() => { | |
let isUnmounted = false; | |
someAsyncOperation().then(data => { | |
if (!isUnmounted ) | |
{ | |
setState(data) | |
} | |
}) | |
return () => { isUnmounted = true}; | |
}, []); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function useMultiRefs(initialValue) { | |
let currentRefs = []; | |
let refs = new Set(currentRefs); | |
function getRefs() { | |
if (initialValue && refs.size === 0) { | |
return initialValue; | |
} | |
if (typeof document === "undefined") { |