[Solved] Subscribing to a client-only collection

I maintain the state of my front-end in several Mongo collections. There are several examples where I want to access the state of an object in two different React components. This is all fine with withTracker.

One further thing I would like to do is maintain a local sub-state - a set of parameters that are not synced with the server, and disappear on page refresh. My first thought would be a client-only unmanaged collection (with connection = null), but I don’t seem to be able to subscribe to changes to this collection.

Would someone be able to point me in the right direction as to whether this is possible, and how to achieve it?

Thanks in advance.

You can only subscribe to a publication. By definition, a client-only collection has no publications.

However, you don’t need to subscribe - the data in client-only collections is synchronous and available immediately, in much the same way as a POJO or array.

1 Like

The problem is that I still want a React component somewhere else in the hierarchy to react to any updates to that collection (i.e., using some equivalent of withTracker).

You can use withTracker, just query the collection directly without subscribing. The query will be reactive, so you will get the desired behavior without using a subscription.

4 Likes

I’m sure I tried that. Let me test again.

Thanks, that works a treat. I’ve no idea what I was doing wrong in the first instance.