Skip to main content
The useStateTogetherWithPerUserValues hook allows users to share state while also being able to read the individual state values of all their peers. Each user maintains their own state value, but can see everyone else’s values in real-time. If the user is not connected to any session, the hook behaves like a normal useState, and the peer state object will be empty.

Basic Usage

Signature

Parameters

string
required
The key used to identify this state across all users in the session.
T
required
The initial value to use when the state is first created.
UseStateTogetherWithPerUserValuesOptions
Configuration options for the hook behavior. See Options below.

Return Values

T
The current local state value for this user.
(T | (T) => T) => void
The setter function that allows updating the local state value.
Record<string, T>
An object containing a mapping between each user ID and their current state value. Users that are not currently rendering this hook will not appear in the mapping, even if they are connected to the session.

Options

boolean
default:"false"
If true, the user’s state will be persisted in the session even after disconnection.
boolean
default:"false"
If true, the local value will not be included in the allValues object.
boolean
default:"false"
By default, when a user connects to a session and a value associated with the user’s identifier already exists (either a persisted value or another user with the same identifier is already connected), the connecting user will update their local state to match the session value. If this flag is true, the user will force its local value into the session.
boolean
default:"false"
If true, the user’s state will be reset to initialValue when the user connects to the session.
boolean
default:"false"
If true, the user’s state will be reset to initialValue after the user disconnects from the session. This only affects the user’s local state after disconnection.
number
default:"100"
The delay in milliseconds between consecutive updates to the state. This only applies when the user is connected to a session.

Examples

Interactive Score System

Create a collaborative scoring system where each user can track their own score while seeing everyone else’s:

Team Progress Tracker

Track individual progress within teams while showing overall team status:

Mood Board Collaboration

Allow users to share their mood while seeing everyone else’s emotional state:

Voting System

Implement a real-time voting system where users can see all votes as they come in:

Best Practices

State Structure Design

  • Keep individual states small and focused - Each user’s state should represent a single concept
  • Use meaningful initial values - Choose defaults that make sense when users first join
  • Consider data normalization - For complex objects, consider separate hooks for different concerns

Performance Optimization

  • Use throttling for frequent updates - Adjust throttleDelay based on your use case
  • Minimize object mutations - Create new objects rather than mutating existing ones
  • Filter displayed data - Only render active users to avoid UI clutter

User Experience

  • Provide visual feedback - Clearly indicate which values belong to which users
  • Handle edge cases - Account for users joining/leaving mid-session
  • Show connection status - Let users know when their updates are being shared

TypeScript Support

This hook is fully typed and will infer the type of your state from the initialValue parameter: