Skip to content
← Snippets

useMounted

Avoid weird SSR issues whether you're rendering on the client or server

Sep 11, 2022 · 1 min read

export const useMounted = () => {
  const [mounted, setMounted] = useState(false);
 
  useEffect(() => {
    setMounted(true);
  }, []);
 
  return mounted;
};

Usage

const SomeClientSideOnlyComponent = () => {
  const mounted = useMounted();
 
  if (!mounted) {
    return null;
  }
 
  return <div>This is client only!</div>;
};
useMounted | Viren Soni | Software Engineer