useMounted React hook

Created: 2021-04-04

The hook

Check if your component has mounted. This can be useful if you're using server side rendering.

import * as React from "react";

export function useMounted() {
  const [isMounted, setMounted] = React.useState(false);

  React.useEffect(() => {
    setMounted(true);

    return () => {
      setMounted(false);
    };
  }, []);

  return isMounted;
}