You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Problem: Losing Unsaved Form Changes in a Multistep Form
Stack:
react
react-hook-form
react-query
Context:
We have implemented a multistep form that allows users to configure a tracking_item and add submarine_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 tracking_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 the tracking_item.
When a new submarine_item is added, we perform a mutation and invalidate the tracking_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:
User navigates to Step 1 and enters a title and description.
User saves the data, which persists in the backend.
User navigates to Step 2 and adds a submarine_item.
Since a mutation occurred, we invalidate the tracking_item query, fetching the latest data and updating the state.
User returns to Step 1 and changes the title but does not save.
User moves back to Step 2 and adds another submarine_item.
Query invalidation happens again, and the form gets repopulated with the latest data from the API.
The unsaved title change is lost because the form gets reset with data from the API.
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 example isFirstCall or something like that).
Code:
exportconstMultiStep=()=>{constqueryClient=useQueryClient()const[step,setStep]=useState(0)const{ setValue }=useForm({defaultValues: {title: '',description: '',items: [{text: ''}],},})const{ data }=useQuery({queryKey: ['tracking_items'],queryFn: ()=>// mocked valuesPromise.resolve({title: 'new_title',description: 'new_description',items: [{text: 'my_item'}],}),})/* Here is the problem, as we always populate values with the newest data. How can we populate it only once? */useEffect(()=>{if(data){setValue('title',data.title)setValue('description',data.description)setValue('items',data.items)}},[data,setValue])return(<>{step===0 ? (<FirstSteponDetailsSave={()=>queryClient.invalidateQueries({queryKey: ['tracking_items']})}/>) : null}{step===1 ? (<SecondSteponItemAdd={()=>queryClient.invalidateQueries({queryKey: ['tracking_items']})}/>) : null}</>)}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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