supabase-cache-helpers
supabase-cache-helpers copied to clipboard
useSubscriptionQuery() does not update subscription when hook parameters change
Describe the bug
useSubscriptionQuery() and useSubscription() setup the Supabase Realtime subscription inside of a useEffect hook with an empty dependencies array.
This means that any change to the parameters passed (e.g. the table name, events, schema, filter, etc.) will NOT cause the actual subscription to be updated and the subscription will continue to notify events only for the first set of parameters passed.
To Reproduce
- Setup a subscription with a filter which is derived from a
setStatevariable. - Update the state variable to a new value (e.g. with input field)
- Make changes in the DB which should only show up for the old or new filters
- See that it always reports changes for the oldest filter param.
Expected behavior
Changes to: channelName, event, schema, table, or filter parameters should cause the subscription to be reset (e.g. unsubscribe and resubscribe).
Additional context
The problem is particularly acute with the usage of filters since this are good use cases for this to be dynamic (e.g. what entity is currently used for active content in the UI). Perhaps only using channelName as the dependency would be sufficient if documentation reflected the necessity of updating channelName when you want
Thanks for opening the issue! I like the idea of using the channel name to reset the subscriptions. Would you be willing to contribute this fix and a documentation? 🙏🏼