Skip to main content
The useLeaveSession hook returns a function that, when called, disconnects the user from the current React Together session. If the user is not connected to any session, calling this function has no effect. When leaving a session, state from useStateTogether and useStateTogetherWithPerUserValues will be preserved locally. Additionally, any session parameters in the window URL will be automatically removed.

Basic Usage

Signature

Return Value

() => void
A function that, when called, disconnects the user from the current React Together session.

Examples

Confirmation Dialog

Add a confirmation dialog before leaving the session:

Session Control Panel

Create a comprehensive session control interface:

Auto-Leave on Inactivity

Automatically leave sessions after a period of inactivity:

Leave with State Save

Save important state before leaving the session:

Leave Feedback

Collect feedback when users leave sessions:

Best Practices

User Experience

  • Confirmation dialogs - Always confirm before leaving to prevent accidental disconnections
  • State preservation - Inform users that their local state will be preserved
  • Clear feedback - Provide immediate visual feedback when leaving
  • Graceful handling - Handle edge cases like already being disconnected

Data Management

  • Save critical data - Save important work before leaving
  • Local storage backup - Consider backing up data to local storage
  • State synchronization - Ensure state is properly synchronized before leaving

Session Management

  • URL cleanup - The hook automatically removes session parameters from the URL
  • Connection state - Monitor connection state to provide accurate user feedback
  • Error handling - Handle network errors gracefully during disconnection

State Behavior

When leaving a session:

Preserved State

  • State from useStateTogether hooks remains in local memory
  • State from useStateTogetherWithPerUserValues keeps your user’s value
  • Local component state is unaffected

Cleaned Up

  • Session parameters are removed from the URL
  • Network connections are closed
  • Real-time synchronization stops

Example State Transition

Common Patterns

Quick Leave

Confirmed Leave

Save and Leave

TypeScript Support

This hook has a simple, type-safe signature:
Calling leaveSession() when not connected to any session has no effect and will not throw an error.