create_user_state

create_user_state creates a shared state hook scoped to the current effective user. Like create_global_state, the state is shared across all components that call the returned hook — but each user gets their own independent state. When User A updates a value, only User A’s components re-render; User B’s components remain unaffected.

Call create_user_state at module level (outside of any component) to create a store. Then call the returned hook inside @ui.component functions to subscribe.

When all of a user’s components using a shared store unmount, that user’s state resets to the initial value.

Examples

Basic example

In this example, each user sees their own name. If User A types “Alice”, User B still sees “stranger” until they type their own name.

Recommendations

  1. Create stores at module level: Call create_user_state at module level, not inside a component. The returned hook is then used inside components.
  2. Naming convention: Name the returned hook starting with use_, e.g. use_user_preference = ui.create_user_state(default).
  3. Use for user-specific data: Preferences, selections, UI state that should differ per user.
  4. Use create_global_state for shared data: If you want all users to share the same value (e.g., a global configuration), use create_global_state instead.

Community vs. Enterprise

On Deephaven Enterprise, create_user_state uses deephaven_enterprise.auth_context.get_effective_user() to identify the current user. Each user gets independent state.

On Deephaven Community (where deephaven_enterprise is not installed), all callers share a single anonymous state — effectively behaving the same as create_global_state. This allows you to write code that works in both environments without modification.

Per-user selection tracking

Custom hooks

You can wrap the hook returned by create_user_state to build a custom hook with prepackaged behavior:

Components call use_messages() and get back add and clear functions instead of a raw setter. Each user’s messages are independent — on Enterprise, User A and User B see different lists.

Cleanup behavior

When all components for a given user unmount, that user’s state resets to the initial value and the internal store for that user is cleaned up. This prevents stale state across sessions and avoids memory leaks when users disconnect.

Thread safety

create_user_state is thread-safe. Multiple users’ components can safely read and update their state concurrently.

API Reference

On Deephaven Community (without deephaven_enterprise), all callers share a single anonymous store, behaving the same as create_global_state.

Returns: Callable[[], tuple[TypeVar(T), Callable[[TypeVar(T) | Callable[[TypeVar(T)], TypeVar(T)]], None]]] A hook function that returns a (value, set_value) tuple, matching the use_state interface. The value and setter are shared across all components for the same effective user.

ParametersTypeDefaultDescription
initial_valueTypeVar(T) |
Callable[[], TypeVar(T)]
NoneThe initial value for the shared state, or a callable that returns the initial value. If a callable is provided, it will be invoked once per user when their store is created.