I don’t understand how to use the real-time subscr...
# javascript
k
I don’t understand how to use the real-time subscription in react. I copied the examples from the docs into a function that gets called in useEffect. It doesn’t seem to be using the callback.
w
Could you post the link to the example?
k
k
im no expert learning my self
but
const getTasks = () => {
Copy code
const getTasks = () => {
        const sub = supabase
            .from('*')
            .on('*', payload => {
                console.log('Change received!', payload)
            })
            .subscribe()
console.log(sub);
            return sub;
do you need to setTask((prevTask) => [payload.new, ...prevTask]);
k
My problem was that I didn’t know you had to mark tables for real-time. That art ticket on Nextvwas helpful @Keooo. Now I’m trying to figure out how to load the current tasks on page load. I’m guessing I have to do that manually because if duedbt look like they’re a listen event for that?
k
from what it looks like it will show a most recent task
k
Is there a way to load previous data when the subscription starts?
k
pretty sure you can
if you console log you subscription you should see the new and the old states
s
That will only trigger when a record is updated/inserted/deleted though. Retrieving on page-load would be done with
supabase.from('my_table').select( /*...*/)
- part of the regular select lookup and independent of the subscription
k
Working on implementing the react-Supabase books. Hopefully it’ll make things easier. Thanks for the help.