fireproof-storage / fireproof-storage/fireproof

`useDocument`'s save callback loses data from any immediately-preceding merges

Open
#1,649 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
973
Forks
58
PR merge metrics
No merged PRs in 30d

Description

As I was first starting out I tried to merge and then save a document immediately via code like:

```
{
let data = new FormData(evt.target);
merge(Object.fromEntries(data));
save();
evt.preventDefault();
}}
>

```

But this saves only the initial document rather than the updated one! I don't see any of the new data from the `merge` call in my live query or when I refresh, the `merge` basically just seems to have gotten lost during the `save`.

One workaround would be after a `merge` to wait until until the component has re-rendered and then use the updated `save` callback. Most of the demos in the docs do ± this because they call `merge(…)` in the oninput/onchange handlers of the form fields, and then `save()` by itself in a separate event handler.

---

The `save` callback is bound to the original doc by default:

https://github.com/fireproof-storage/fireproof/blob/b5f258c7519a8d4fc41f255ec06284e2263e4243/use-fireproof/base/react/use-document.ts#L75-L88

And the merge method does not expose the new doc: https://github.com/fireproof-storage/fireproof/blob/b5f258c7519a8d4fc41f255ec06284e2263e4243/use-fireproof/base/react/use-document.ts#L104-L107

I need to get more familiar with the intended `useDocument` mental model overall, but I wonder if it should be tracking a working copy of the document as a ref beyond just the `useState` so that the various callbacks stay in sync when used together. Might be vaguely semi-related to #1188 mainly just in the UX sense of things a new user expects to be able to do, although in this case I had noticed that I was intended to `save()`. Just didn't realize I had to wait for a fresh render between updating and saving.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.