Skip to content

Add option to skip or disable initial mutations in usePresence #16

@pneumaticos

Description

@pneumaticos

Currently, usePresence automatically performs presence mutations (e.g., joining/leaving or updating presence data) as soon as it’s initialized.

In some cases, it would be helpful to conditionally start presence tracking—for example, when the required identifiers (like user or room) are not yet available, or when presence should only be synced after a user action.

Proposed solution:
Add an optional enabled or skip parameter to usePresence, similar to the enabled flag used in React Query hooks.

const presence = usePresence(api.presence, roomName, userName, {
  enabled: !!(roomName && userName),
});

When enabled is false, the hook would skip running the presence join/update mutations and only start syncing once it’s toggled true.

Benefits:

Avoids unnecessary mutations or presence records with undefined data.

Matches common React patterns for conditional hooks (e.g., enabled in React Query or SWR).

Cleaner than workarounds with conditional hook arguments or dummy values.

Example use case:

const { user, restaurant } = useAdmin();
const presence = usePresence(api.presence, restaurant?.name, user?.name, {
  enabled: !!(restaurant && user),
});

This would ensure the hook only activates once both values are ready.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions