supabase-cache-helpers icon indicating copy to clipboard operation
supabase-cache-helpers copied to clipboard

useSubscriptionQuery() does not update subscription when hook parameters change

Open adamstoffel opened this issue 1 year ago • 1 comments

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

  1. Setup a subscription with a filter which is derived from a setState variable.
  2. Update the state variable to a new value (e.g. with input field)
  3. Make changes in the DB which should only show up for the old or new filters
  4. 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

adamstoffel avatar Nov 09 '24 01:11 adamstoffel

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? 🙏🏼

psteinroe avatar Nov 09 '24 12:11 psteinroe