Skip to main content
import { useEffect, useState } from 'react';
import { View, Text } from 'react-native';
import { useStytchB2BClient } from '@stytch/react-native/b2b';

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

  useEffect(() => {
    // Subscribe to session changes
    const unsubscribe = stytch.session.onChange((session) => {
      setSession(session);
    });

    // Cleanup subscription on unmount
    return unsubscribe;
  }, [stytch]);

  return (
    <View>
      {session ? (
        <Text>Session ID: {session.member_session_id}</Text>
      ) : (
        <Text>No active session</Text>
      )}
    </View>
  );
};
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 Organization change event.