Complete the code to import the React hook used for state management.
import React, { [1] } from 'react';
The useState hook is used to manage state in React components.
Complete the code to subscribe to a Supabase realtime channel inside a React effect hook.
useEffect(() => {
const channel = supabase.channel('public:messages').on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'messages' }, payload => {
setMessages(current => [...current, payload.new]);
});
channel.[1]();
return () => {
channel.unsubscribe();
};
}, []);The subscribe() method starts listening to the realtime channel events.
Fix the error in the cleanup function to properly unsubscribe from the Supabase channel.
return () => { channel.[1](); };
The unsubscribe() method properly stops listening to the channel events.
Fill both blanks to update state with new realtime data and ensure the effect runs only once.
useEffect(() => {
const channel = supabase.channel('public:notifications').on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'notifications' }, payload => {
[1](current => [...current, payload.new]);
});
channel.subscribe();
return () => {
channel.unsubscribe();
};
}, [2]);setNotifications updates the notifications state.
[] as dependency array makes the effect run only once on mount.
Fill all three blanks to create a realtime subscription that listens for DELETE events and updates state accordingly.
useEffect(() => {
const channel = supabase.channel('public:tasks').on('postgres_changes', { event: '[1]', schema: 'public', table: 'tasks' }, payload => {
[2](current => current.filter(task => task.id !== payload.old.[3]));
});
channel.subscribe();
return () => channel.unsubscribe();
}, []);DELETE listens for delete events.
setTasks updates the tasks state.
id is the unique identifier to filter out the deleted task.
