Subscriptions
Subscriptions work similarly to other reactive JS frameworks.
You can subscribe to a query of Replicache and you will get notified when that query changes for any reason — either because of local optimistic changes, or because of sync.
Subscriptions are Fast
Replicache goes to significant trouble to make reactive renders efficient:
- Replicache only calls the query function (the parameter to subscribe) when any of the keys it accessed last time change.
- The
onData
callback is only called when the result of the query function changes. - Replicache will usually return objects with the same identity across queries, so you can use things like React’s
useMemo
to avoid re-renders.
Challenge
Modify the sample to increment by zero, and verify that the onData
callback is not called. This is because even though the count
key was re-written, its value didn't change. So Replicache didn't call the onData
callback.