Skip to content

Commit

Permalink
okay, tests passing. still more to do
Browse files Browse the repository at this point in the history
  • Loading branch information
pvh committed Jan 5, 2025
1 parent c61e7e4 commit f548d88
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/automerge-repo-react-hooks/src/useDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ export function useDocument<T>(
id: AnyDocumentId
): [Doc<T>, (changeFn: ChangeFn<T>, options?: ChangeOptions<T>) => void] {
const handle = useDocHandle<T>(id)
// Initialize with current doc state
const [doc, setDoc] = useState<Doc<T>>(() => handle.docSync())
const [deleteError, setDeleteError] = useState<Error>()

// Reinitialize doc when handle changes
useEffect(() => {
setDoc(handle.docSync())
}, [handle])

useEffect(() => {
const onChange = () => setDoc(handle.docSync())
const onDelete = () => {
Expand All @@ -54,7 +60,6 @@ export function useDocument<T>(
[handle]
)

// Throw delete error during render
if (deleteError) {
throw deleteError
}
Expand Down

0 comments on commit f548d88

Please sign in to comment.