Mihai Andrei
10/10/2021, 5:46 PMMihai Andrei
10/10/2021, 5:46 PMMihai Andrei
10/10/2021, 5:46 PMgaryaustin
10/10/2021, 5:57 PMMihai Andrei
10/10/2021, 6:02 PMgaryaustin
10/10/2021, 6:12 PMNull
10/11/2021, 4:43 PMNull
10/11/2021, 4:43 PMjs
useEffect(() => {
const mySubscription = supabase
.from("posts")
.on("INSERT", (payload) => {
console.log(posts);
})
.subscribe();
return () => {
supabase.removeSubscription(mySubscription);
};
}, []);
Null
10/11/2021, 4:45 PMMihai Andrei
10/11/2021, 4:47 PMNull
10/11/2021, 4:52 PMNull
10/11/2021, 4:52 PMjs
const Messages = () => {
const [posts, setPosts] = useState([]);
const router = useRouter();
const { id } = router.query;
useEffect(() => {
fetchPosts();
}, [id]);
useEffect(() => {
console.log(posts);
}, [posts]);
const fetchPosts = async () => {
let { data, error } = await supabase.from("posts").select("*").eq("room", id);
error ? console.log(error) : setPosts(data);
};
useEffect(() => {
const mySubscription = supabase
.from("posts")
.on("INSERT", (payload) => {
console.log(posts);
})
.subscribe();
return () => {
supabase.removeSubscription(mySubscription);
};
}, []);
return (
<div>
{posts.map((post) => {
return <p key={post.id}>{post.content}</p>;
})}
</div>
);
};
Null
10/11/2021, 4:53 PMNull
10/11/2021, 4:53 PMconst [posts, setPosts] = useState([]);
Null
10/11/2021, 4:54 PMMihai Andrei
10/11/2021, 5:53 PMMihai Andrei
10/11/2021, 5:53 PMMihai Andrei
10/11/2021, 5:53 PMMihai Andrei
10/11/2021, 5:55 PMjs
useEffect(() => {
const mySubscription = supabase
.from("posts")
.on("INSERT", (payload) => {
console.log(posts);
})
.subscribe();
return () => {
supabase.removeSubscription(mySubscription);
};
}, [posts]);
Mihai Andrei
10/11/2021, 5:55 PMNull
10/11/2021, 8:24 PMsetPosts([...posts, payload.new]).
I was using the console log to debug and thats how I found out why I wasn't getting back the array spread, it comes up blank from its default value. And adding posts into the dependencies wouldn't work due to it not changing.Null
10/11/2021, 8:24 PMNull
10/11/2021, 8:26 PMMihai Andrei
10/11/2021, 8:48 PMMihai Andrei
10/11/2021, 8:48 PMMihai Andrei
10/11/2021, 8:48 PMMihai Andrei
10/11/2021, 8:50 PMjs
useEffect(() => {
const mySubscription = supabase
.from("posts")
.on("INSERT", (payload) => {
this.handleInsert(payload)
})
.subscribe();
return () => {
supabase.removeSubscription(mySubscription);
};
}, [posts]);
Const handleInsert = (payload) => {
setPosts(prevPosts => […prevPosts, payload.new])
}
Null
10/11/2021, 8:57 PMthis
doing?Null
10/11/2021, 8:57 PMthis
.handleInsert(payload)Mihai Andrei
10/11/2021, 9:00 PMMihai Andrei
10/11/2021, 9:00 PMMihai Andrei
10/11/2021, 9:00 PMMihai Andrei
10/11/2021, 9:00 PMMihai Andrei
10/11/2021, 9:00 PMNull
10/11/2021, 9:02 PMMihai Andrei
10/11/2021, 9:04 PMNull
10/11/2021, 9:05 PMNull
10/11/2021, 9:05 PMNull
10/11/2021, 9:09 PMjs
useEffect(() => {
const mySubscription = supabase
.from("posts")
.on("INSERT", (payload) => {
handleInsert(payload);
})
.subscribe();
return () => {
supabase.removeSubscription(mySubscription);
};
}, [posts]);
const handleInsert = (newPost) => {
setPosts([...posts, newPost.new]);
console.log(posts);
};
Null
10/11/2021, 9:09 PMNull
10/11/2021, 9:09 PMMihai Andrei
10/11/2021, 9:12 PMMihai Andrei
10/11/2021, 9:12 PMNull
10/11/2021, 9:12 PMNull
10/11/2021, 9:12 PMNull
10/11/2021, 9:12 PMMihai Andrei
10/11/2021, 9:12 PMNull
10/11/2021, 9:13 PMjs
setPosts((prevPosts) => [...prevPosts, newPost.new]);
Null
10/11/2021, 9:13 PMNull
10/11/2021, 9:14 PMNull
10/11/2021, 9:14 PMsetPosts([...posts, newPost.new]);
Mihai Andrei
10/11/2021, 9:14 PMNull
10/11/2021, 9:14 PMNull
10/11/2021, 9:14 PMNull
10/11/2021, 9:14 PMMihai Andrei
10/11/2021, 9:14 PMMihai Andrei
10/11/2021, 9:15 PMMihai Andrei
10/11/2021, 9:15 PMMihai Andrei
10/11/2021, 9:15 PMMihai Andrei
10/11/2021, 9:15 PMMihai Andrei
10/11/2021, 9:16 PMNull
10/11/2021, 9:16 PMMihai Andrei
10/11/2021, 9:18 PM