session.onChange method to listen for and react to changes to the Session object.
Parameters
The callback function to call when the session changes. The first parameter is the updated Session object.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
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>
);
};
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>
);
};
Listen for changes to the current Session using the Stytch Next.js SDK
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>
);
};
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>
);
};
session.onChange method to listen for and react to changes to the Session object.
Show parameters
Show properties