dotUI
dotUI
beta
  1. Hooks
  2. Browser hooks
  3. useIsClient

useIsClient

Determine whether the code is running on the client-side or server-side with useIsClient.

Installation

Copy and paste the following code into your project.

import React from "react";

export function useIsClient() {
  const [isClient, setIsClient] = React.useState(false);

  React.useEffect(() => {
    setIsClient(true);
  }, []);

  return isClient;
}

Update the import paths to match your project setup.

Usage

import { useIsClient } from "@/hooks/use-is-client";

function MyComponent() {
  const isClient = useIsClient();

  return <div>{isClient ? "Client-side" : "Server-side"}</div>;
}

Return value

NameTypeDescription
isClient
boolean
true if component is mounted, false otherwise.

Built by mehdibha. The source code is available on GitHub.