Skip to content

このページは翻訳の準備中のため、英語の原文を表示しています。

useIsClient

useIsClient is a React hook that returns true only in the client-side environment. It is primarily used to differentiate between client-side and server-side rendering (SSR). The state is set to true only after the component is mounted in the client-side environment.

Interface

ts
function useIsClient(): boolean;

Parameters

Return Value

  • boolean

    true in a client


Example

tsx
function ClientSideContent() {
  const isClient = useIsClient();

  if (!isClient) {
    return <div>Loading...</div>; // Rendered on the server side
  }

  return <div>Client-side rendered content</div>; // Rendered on the client side
}

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

  if (!isClient) return null;

  return <div id="map" />;
}

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

  const theme = isClient ? localStorage.getItem('theme') : 'light';

  return <div>Current theme: {theme}</div>;
}

MIT ライセンスの下で配布されています。