Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

replicache-react

Package Overview
Dependencies
Maintainers
6
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

replicache-react

Miscellaneous utilities for using Replicache with React

  • 5.0.1
  • canary
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.3K
decreased by-44.55%
Maintainers
6
Weekly downloads
 
Created
Source

replicache-react

npm version


npm i replicache-react

Build your UI using subscribe() (or useSubscribe in React). Whenever the data in Replicache changes — either due to changes in this tab, another tab, or on the server — the affected UI automatically updates.
Replicache only refires subscriptions when the query results have actually changed, eliminating wasteful re-renders.

API

function useSubscribe

React hook that allows you monitor replicache changes

ParameterTypeDescription
repReplicacheReplicache instance that is being monitored
query(tx: ReadTransaction) => PromiseQuery that retrieves data to be watched
defRdefault value returned on first render or whenever query returns undefined
depsArray = []OPTIONAL: list of dependencies, query will be rerun when any of these change

Usage

example of useSubscribe in todo app that is watching a specific category

const {category} = props;
const todos = useSubscribe(
  replicache,
  tx =>
    tx
      .scan({prefix: `/todo/${category}`})
      .values()
      .toArray(),
  [],
  [category],
);

return (
  <ul>
    {todos.map(t => (
      <li>{t.title}</li>
    ))}
  </ul>
);

Changelog

0.4.0

This release changes the semantics of def slightly. In previous releases, def was returned only until query returned, then useSubscribe returns query's result. Now, def is returned initially, but also if query returns undefined.

This is an ergonomic benefit because it avoids having to type the default in two places. Before:

useSubscribe(r, tx => (await tx.get('count')) ?? 0, 0);

now:

useSubscribe(r, tx => tx.get('count'), 0);

FAQs

Package last updated on 01 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc