Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Last active November 9, 2020 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/156efd21389050870313d0cf9356be70 to your computer and use it in GitHub Desktop.
Save kellenmace/156efd21389050870313d0cf9356be70 to your computer and use it in GitHub Desktop.
React component that returns true once the component/hook using it has mounted
import React, { useState, useEffect } from "react"
/**
* @return {boolean} Whether the component has mounted.
*/
function useHasMounted() {
const [hasMounted, setHasMounted] = useState(false)
useEffect(() => {
setHasMounted(true)
}, [])
return hasMounted
}
export default useHasMounted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment