Replies: 7 comments 26 replies
-
relevant: #2335 |
Beta Was this translation helpful? Give feedback.
-
I have to admit I haven't fully understood the
However, what is missing is that you can keep your own input on big forms, but get untouched fields updated by background updates. What I would like to see is a way to sync only the
now if we have changed the name already, this wouldn't do anything. Maybe, if we have changed the name to If we have set the name to I hope I'm making somewhat sense 😅 |
Beta Was this translation helpful? Give feedback.
-
reset(data, {
keepDirtyValues: true
}) I think this API is probably closest to the existing pattern. options to have a new |
Beta Was this translation helpful? Give feedback.
-
I guess the solution with the current API is this weird setup (reusing the logic from the initial "solution" example from the OG post) useEffect(() => {
if (user) {
const newDefaultValues = Object.fromEntries(
Object.entries(user).filter(([userKey]) => !dirtyFields[userKey])
)
const currentValues = getValues()
reset(user, {
keepValues: true
})
reset(
{
...currentValues,
...newDefaultValues
},
{
keepDefaultValues: true
}
)
}
}, [user, dirtyFields, getValues, reset]) It seems to work as expected. I know it's not pretty, but that's what I can come up with at this time. reset-screencast.mp4 |
Beta Was this translation helpful? Give feedback.
-
Hi @Moshyfawn ! Out of curiosity, have you discovered a useful way of handling server state with For example, say that your server stores a collection of 2 items to display within your form (to do this we'll utilize Here's a codesandbox which shows this issue: https://codesandbox.io/s/react-keepdirtyvalues-forked-jyezhw?file=/src/App.tsx Ideally my example would load in data and
Any thoughts on the best approach for handling this scenario? |
Beta Was this translation helpful? Give feedback.
-
Hi all. Reading this thread reminds me a lot about our autosave implementation. We allow editing of forms, where the server can occasionally compute new values based on what the user submitted, and we want to update the form with those new values, but not erase any unsynced changes. Here's our implementation. I know for sure it's not very good, and has a small bug in it (have yet to solve it), but it works for the most part. https://gist.github.com/dbousamra/37d67e2f7b43261d424e7b0d75032d5b There's some extra stuff in here we do around optimistic locking (taking ownership), that can be ignored. |
Beta Was this translation helpful? Give feedback.
-
Man, I'm used to random usernames with two names followed by four numbers trying to add me and spam me on every service that isn't GitHub. Now I have to deal with them on GitHub? For anyone else that shows up there isn't aware the previous comment is spam probably trying to get you to go to some shady website to call you out of money that you'll give them probably via gift cards. Don't click on the links in the comment mentioning a bunch of usernames above and I already reported it so if you would report it to that may speed things up to get it removed. Edit: well that was quick. Comment is already deleted. Everything on this thread is probably safe now. |
Beta Was this translation helpful? Give feedback.
-
Intro
This is a big topic about managing a close-to-real-time (in some cases, actually real-time) form data. I think it's important to introduce a solid way to sync the remote data with the currently active (editable) form.
The current state of things
At this time, React Hook Form is focused on "traditional" more Redux-like type of data flow (a very stretch comparison, IK. Gotta anchor to smth lol): you fetch some
async
default values andreset
the form when you get the data, not thinking about the remote data anymore; you've set the client-side form values and operate the client state, assuming the remote data never changes without the current form submission (mutating remote data on form submission)The problem
The issue arises when the data becomes more "unstable": introduction of background re-fetches, stale data,
subscriptions
, etc.The JS eco-system is greatly advancing in the field of cache management, back-end - front-end data
sync
ing. Just look at packages like SWR and Apollo. Even "the one store - 99 problems" Redux is moving towards dividing the data cache and the client store: RTK Query.I'm going to use the React Query cache management (and a ton more) package as a base to showcase the ever-changing data use-case for simplicity-sake.
Consider some back-end data entry you can update (mutate) via your form on the client (your basic CRUD, if you will):
The potential form setup to update the entry with React Hook Form can look like this:
Now, our data is stored and the back-end and we need to retrieve it to initialize our form with the actual default values with out entry data. We don't have the needed data to be set as form's
defaultValues
initially. As I've mentioned previously, the current React Hook Form recommended pattern is toreset
the form's values as soon as ourasync
data comes in. Let's try applying it to our form (Keep in mind that we're not looking at such concepts assuspense
as it's a whole another topic)Provided our back-end user data changes only when we submit the form successfully, problem solved.
Introducing background re-fetch. I won't go into great detail about how it works or what it means for our form. For that you can read up the @TkDodo's blog-post React Query and Forms. For our form, background refetch means that our client state form data will be eventually
synced
with the remote data during the life-time of our form, meaning, with our current setup the user form input will bereset
with the back-end data, which can potentially differ; not a greate UX at all.You can check out this codesanbox which demonstrates our current issue:
The current solution
There's a lot going on and there're multiple moving pieces to this problem. There's no one way to tackle this topic.
It's tough to solve an issue, the solution to which depends on the business requirements, app architecture, performance, etc. etc. Simply put: it's case-by-case study. To list a few:
sync
ing client and server stateThat said, there're a couple of things you can decide on the form's side.
The solution that comes to mind is to check the
dirtyFields
values and override only the yet to be modified fields, leaving the dirty fields with the current user-entered values. This would look something likeHere's a codesanbox to play around with it live
This approach while being a bit convoluted, allows to us to override only the currently unmodified form fields, leaving the user's progress alone. Is it the solution? No, it leaves a lot to be desired in terms of UX: updating form values, even the user hasn't yet touched them can easily get confusing.
@TkDodo provides a slightly different approach to managing the form values updates using the controlled inputs. You can see his example here.
The proper solution would combine something from the method mentioned above with potentially a better React Hook Form API to update the intended fields + the user notification system + potentially a completely different approach + ...
You can notice how untapped this question is. Hence we need your help :P
Outro
I'm very curious to hear your opinion on the matter. Please, share you experience and potential solutions you might have came up with, so we can all discuss it and improve React Hook Form and sequentially the developer's quality of life (your life, lol)
P.S. For those who are not familiar with the cache management concept described above, please, take a look at the greatest information source there's about React Query and back-end data synchronization in general: @TkDodo's blog. Or feel free to ask me questions on the topic here.
P.P.S. Here's a list of discussion resources:
keepDirtyOnReinitialize
flag" Twitter threadP.P.P.S. Sry for a long and crumpled description. it's such a big topic; my little brain doesn't know how to bite into it at times xD
Beta Was this translation helpful? Give feedback.
All reactions