Skip to main content

offEvent

Removes an event listener from Dynamic client events. This function unsubscribes a previously registered event listener from the specified Dynamic client event.

Usage

import { offEvent, onEvent } from '@dynamic-labs-sdk/client';

// Create listener function
const handleLogout = () => {
  console.log('User logged out');
};

// Add listener
onEvent({ event: 'logout', listener: handleLogout });

// Later, remove the listener
offEvent({ event: 'logout', listener: handleLogout });

Parameters

ParameterTypeDescription
eventstringThe event name to remove the listener from.
listenerFunctionThe callback function to remove.
clientDynamicClient (optional)The Dynamic client instance. Only required when using multiple clients.

Returns

void

Important Notes

  • The listener parameter must be the exact same function reference that was used when adding the listener
  • Anonymous functions cannot be removed with offEvent - store a reference to the function first

Examples

Basic event cleanup

import { onEvent, offEvent } from '@dynamic-labs-sdk/client';

const logoutHandler = () => {
  console.log('User logged out');
  // Clean up resources
};

// Subscribe to event
onEvent({ event: 'logout', listener: logoutHandler });

// Unsubscribe when no longer needed
const cleanup = () => {
  offEvent({ event: 'logout', listener: logoutHandler });
};

Using returned cleanup function from onEvent

import { onEvent } from '@dynamic-labs-sdk/client';

// onEvent returns a cleanup function, which is often more convenient
const unsubscribe = onEvent({
  event: 'logout',
  listener: () => console.log('Logout!'),
});

// Later, call the returned function to unsubscribe
unsubscribe();