Given a React component using Supabase's realtime subscription with hooks, what will be the output in the console when a new row is inserted into the subscribed table?
import { useEffect } from 'react'; import { createClient } from '@supabase/supabase-js'; const supabase = createClient('https://xyzcompany.supabase.co', 'public-anon-key'); function RealtimeComponent() { useEffect(() => { const subscription = supabase .from('messages') .on('INSERT', payload => { console.log('New message:', payload.new); }) .subscribe(); return () => { subscription.unsubscribe(); }; }, []); return null; }
Think about what the 'on' method with 'INSERT' event listens for and what 'payload.new' contains.
The subscription listens for new rows inserted into the 'messages' table. When a new row is inserted, the callback logs the new row object, which is accessible via 'payload.new'. The subscription is active because it is created inside useEffect with an empty dependency array, and the cleanup removes it properly.
Which option correctly cleans up the Supabase realtime subscription inside a React useEffect hook to avoid memory leaks?
useEffect(() => {
const subscription = supabase
.from('notifications')
.on('UPDATE', payload => {
console.log('Updated:', payload.new);
})
.subscribe();
// Cleanup function here
return () => {
// ???
};
}, []);Check the Supabase client API for unsubscribing from realtime subscriptions.
The correct way to clean up a subscription is to call 'unsubscribe()' on the subscription object returned by 'subscribe()'. The method 'removeSubscription' is deprecated. The other options either call methods on incorrect objects or do not exist.
You want to build a realtime notification system using Supabase and React hooks. Which architectural choice best ensures minimal resource usage on the client while keeping notifications up-to-date?
Consider how to reduce unnecessary data sent to the client and avoid excessive network usage.
Subscribing only to relevant notifications using filtered realtime channels reduces data sent to the client and resource usage. Polling wastes resources and delays updates. Subscribing to all changes causes unnecessary data processing. Fetching once does not keep data realtime.
How can you ensure that a user only receives realtime updates for rows they are authorized to see in Supabase?
Think about how Supabase enforces data access control on the server side.
Enabling RLS with proper policies ensures that only authorized rows are sent to the client via realtime subscriptions. Client-side filtering is insecure and inefficient. Disabling RLS exposes data to unauthorized users.
Which approach best prevents unnecessary re-renders in a React component subscribed to Supabase realtime updates?
Consider how React re-renders components when state changes and how to minimize updates.
Using useRef to hold the latest data avoids triggering re-renders on every event. Updating state only when data meaningfully changes combined with React.memo prevents unnecessary renders. Updating state blindly or recreating subscriptions causes performance issues.
