Hey everyone, I'll start off by saying this is probably more a react thing than a watermelon thing, but i'm not sure which way to go. I've used https://github.com/bndkt/sharemystack and the watermelon docs as a reference and came up with a custom hook:
THE CUSTOM HOOK
"use client";
import { useEffect, useState } from "react";
import { useDatabase } from "@nozbe/watermelondb/hooks";
import type { Post } from "@repo/database";
import { TableName } from "@repo/database";
import { useSync } from "./use-sync";
export function usePost(id: string) {
const database = useDatabase();
const sync = useSync();
const [post, setPost] = useState<Post | null>(null);
useEffect(() => {
const sub = database
.get<Post>(TableName.POSTS)
.findAndObserve(id)
.subscribe((data) => {
console.log("usePost", data);
setPost(data ?? null);
});
return () => sub.unsubscribe();
}, [database, sync]);
return { post };
}
Hey everyone, I'll start off by saying this is probably more a react thing than a watermelon thing, but i'm not sure which way to go. I've used https://github.com/bndkt/sharemystack and the watermelon docs as a reference and came up with a custom hook:
THE CUSTOM HOOK
USING THE HOOK
Any help is appreciated, thanks in advance :)