npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sync-subscribe/client-react

v0.3.2

Published

React bindings for `@sync-subscribe/client`. Provides a context provider and hooks for subscribing to synced data, making mutations, and automatically replaying changes when the device comes back online.

Downloads

17

Readme

@sync-subscribe/client-react

React bindings for @sync-subscribe/client. Provides a context provider and hooks for subscribing to synced data, making mutations, and automatically replaying changes when the device comes back online.

Installation

npm install @sync-subscribe/client-react @sync-subscribe/client @sync-subscribe/core

React ≥ 18 is a peer dependency.

Quick start

import { SyncClient, IdbLocalStore, createFetchTransport } from "@sync-subscribe/client";
import { SyncProvider, useRecords, useMutate } from "@sync-subscribe/client-react";

// Create the client once at module level (outside components)
const client = new SyncClient(
  createFetchTransport({ baseUrl: "/api" }),
  new IdbLocalStore("my-app-db"), // persists across reloads; omit for in-memory
);

function App() {
  return (
    <SyncProvider client={client}>
      <NotesList />
    </SyncProvider>
  );
}

function NotesList() {
  const notes = useRecords<NoteRecord>({ filter: { isDeleted: false } });
  const mutate = useMutate<NoteRecord>();

  async function handleDelete(note: NoteRecord) {
    await mutate({
      ...note,
      isDeleted: true,
      updatedAt: Date.now(),
      revisionCount: note.revisionCount + 1,
    });
  }

  return (
    <ul>
      {notes.map((n) => (
        <li key={n.recordId}>
          {n.title}
          <button onClick={() => handleDelete(n)}>Delete</button>
        </li>
      ))}
    </ul>
  );
}

API

<SyncProvider client={client}>

Provides the SyncClient to all child hooks. Place it once near the root of your app.

The provider also manages the offline mutation queue. Mutations made while the device is offline are written to the local store immediately (read-your-own-writes) and pushed to the server automatically when connectivity is restored.

<SyncProvider client={client}>
  <App />
</SyncProvider>

useRecords<T>(options)

Subscribes to a filtered view of records and returns them as a live array.

const notes = useRecords<NoteRecord>({
  filter: { isDeleted: false },
  name: "active-notes", // optional — persists subscription state across sessions
});

What it does:

  • On mount: registers a subscription locally for filter, pulls the initial batch, and opens an SSE stream
  • Re-renders on every incoming patch (from pull, stream, or conflict resolution)
  • When filter changes: registers a new subscription, replacing the old one. Gap analysis runs locally to determine whether any records matching the new filter are not yet cached — if so they are fetched before joining the stream. Records that only the old filter needed are evicted.
  • Filters the local store client-side with matchesFilter, so multiple overlapping useRecords calls with different filters work correctly

Filter operators:

// Equality
useRecords({ filter: { color: "blue" } });

// Comparison operators
useRecords({ filter: { createdAt: { $gte: Date.now() - 30 * 24 * 60 * 60 * 1000 } } });

// Multiple conditions (all must match)
useRecords({ filter: { isDeleted: false, category: "work" } });

// $or
useRecords({ filter: { $or: [{ color: "blue" }, { color: "red" }] } });

Stable filter references:

useRecords serialises the filter to detect changes, so passing an inline object literal is safe — it won't trigger unnecessary re-subscriptions:

// Fine — does NOT re-subscribe on every render
const notes = useRecords({ filter: { isDeleted: false } });

useMutate<T>()

Returns a mutate function scoped to the nearest SyncProvider.

const mutate = useMutate<NoteRecord>();

mutate(record): Promise<boolean>

  • Writes the record to the local store immediately (read-your-own-writes)
  • If online: pushes to the server; returns true on success, false on conflict (server record wins and is applied locally)
  • If offline: queues the push and returns true optimistically; the push is retried when the device comes back online

Always increment revisionCount on every change:

// Create
await mutate({
  recordId: crypto.randomUUID(),
  createdAt: Date.now(),
  updatedAt: Date.now(),
  revisionCount: 1,
  title: "New note",
  isDeleted: false,
});

// Update
await mutate({
  ...existing,
  title: "Edited title",
  updatedAt: Date.now(),
  revisionCount: existing.revisionCount + 1,
});

// Soft-delete
await mutate({
  ...existing,
  isDeleted: true,
  updatedAt: Date.now(),
  revisionCount: existing.revisionCount + 1,
});

useSyncClient<T>()

Escape hatch to access the raw SyncClient from context.

const client = useSyncClient<NoteRecord>();
await client.reset(); // e.g. on logout

Offline behaviour

SyncProvider listens to the browser's online event. When the device reconnects, it drains the pending queue in the order mutations were made, deduplicating by recordId (only the latest mutation per record is sent).

offline  →  mutate(noteA v1)  →  mutate(noteA v2)  →  online
                                                          ↓
                                               push(noteA v2)   ← only latest sent

If a push fails after reconnecting (e.g. the server is still unreachable), the record is re-queued and retried on the next online event.


Multiple subscriptions

Multiple useRecords calls create independent subscriptions. The local store deduplicates records by recordId, so records matching more than one filter are stored only once. Each hook filters client-side to return its own view.

// Two subscriptions, no duplicates in the local store
const recentNotes = useRecords({ filter: { createdAt: { $gte: thirtyDaysAgo } } });
const blueNotes   = useRecords({ filter: { color: "blue" } });