React Together provides a comprehensive set of hooks for building collaborative React applications. Each hook serves a specific purpose in creating synchronized, multi-user experiences.

Core State Hooks

The foundation of collaborative React applications - these hooks handle shared state synchronization:

Connection & Session Hooks

Manage user connections and session state:

Session Management Hooks

Create, join, and leave collaborative sessions:

Communication Hooks

Enable real-time communication between users:

Utility Hooks

Helper hooks for common patterns and data access:

Hook Categories

By Use Case

By Complexity Level

Common Patterns

Basic Collaborative Component

import { useStateTogether, useConnectedUsers } from 'react-together'

function CollaborativeCounter() {
  const [count, setCount] = useStateTogether('counter', 0)
  const connectedUsers = useConnectedUsers()
  
  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={() => setCount(count + 1)}>
        Increment
      </button>
      <p>{connectedUsers.length} users connected</p>
    </div>
  )
}

Session Management

import { useCreateRandomSession, useJoinUrl, useLeaveSession } from 'react-together'

function SessionControls() {
  const createSession = useCreateRandomSession()
  const joinUrl = useJoinUrl()
  const leaveSession = useLeaveSession()
  
  return (
    <div>
      <button onClick={() => createSession()}>
        Create New Session
      </button>
      {joinUrl && (
        <div>
          <p>Share this URL:</p>
          <code>{joinUrl}</code>
        </div>
      )}
      <button onClick={() => leaveSession()}>
        Leave Session
      </button>
    </div>
  )
}

User Awareness

import { useConnectedUsers, useNicknames, useCursors } from 'react-together'

function UserAwareness() {
  const connectedUsers = useConnectedUsers()
  const nicknames = useNicknames()
  const cursors = useCursors()
  
  return (
    <div>
      <h3>Connected Users:</h3>
      {connectedUsers.map(user => (
        <div key={user.userId}>
          {nicknames[user.userId] || 'Anonymous'}
        </div>
      ))}
      
      {/* Render cursors */}
      {cursors.map(cursor => (
        <div
          key={cursor.userId}
          style={{
            position: 'absolute',
            left: cursor.x,
            top: cursor.y,
            pointerEvents: 'none'
          }}
        >
          👆 {nicknames[cursor.userId]}
        </div>
      ))}
    </div>
  )
}

Next Steps