Replies: 1 comment
-
don’t sync with useEffect, use the react values API from react hook form, as it’s made for this: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem: Losing Unsaved Form Changes in a Multistep Form
Stack:
Context:
We have implemented a multistep form that allows users to configure a
tracking_item
and addsubmarine_items
to it. The form consists of two steps:Step 1: Configure Tracking Item
The user sets the title and description of the
tracking_item
.When the component mounts, we fetch the
tracking_item
using a React Query query.If the t
racking_item
exists, we populate the form fields with its values. Otherwise, the fields remain empty.The user can navigate to Step 2 with or without saving the form.
Step 2: Add Submarine Items
The user adds
submarine_items
to thetracking_item
.When a new
submarine_item
is added, we perform a mutation and invalidate thetracking_item
query to ensure the data stays fresh.The user can navigate back to Step 1 at any time.
Expected Behavior:
If the user enters Step 1, modifies the title, but does not save, we want this change to persist when they return to Step 1.
If they move to Step 2 without saving, add a
submarine_item
, and return to Step 1, their unsaved changes to title should still be there.Actual Problem:
submarine_item
.tracking_item
query, fetching the latest data and updating the state.submarine_item
.Key Issue:
Because query invalidation refetches data, any unsaved changes made in Step 1 are lost when the form is repopulated.
Would you like suggestions on how to solve this? Is there a way to fill the data only once? I don't see any value that
useQuery
returns (for exampleisFirstCall
or something like that).Code:
Beta Was this translation helpful? Give feedback.
All reactions