Skip to main content
import { useEffect, useState } from 'react';
import { useStytch } from '@stytch/nextjs';

export const SessionDisplay = () => {
  const stytch = useStytch();
  const [session, setSession] = useState(null);

  useEffect(() => {
    const unsubscribe = stytch.session.onChange((session) => {
      setSession(session);
    });

    return unsubscribe;
  }, [stytch]);

  return session ? (
    <p>Session ID: {session.session_id}</p>
  ) : (
    <p>No active session</p>
  );
};
The Stytch SDK caches the Session of the logged-in . Use the session.onChange method to listen for and react to changes to the Session object.

Parameters

callback
function
The callback function to call when the session changes. The first parameter is the updated Session object.

Return value

unsubscribe
function
The function to call to unsubscribe from the Session change event.