How to prevent React-Final-Form to reset the form values after user submits the form?
Asked Answered
D

2

5

I am using react-final-form in React App. It's working great!

But is there any way to prevent the react-final-form from resetting the input values of the form after user successfully submit the form.

Reason why I need it because, currently when the user is redirected after submitted the form, user see input values being reset to initial form values for 200-400 micro second between the action of submitting the form and redirecting to other component.

I tried using following but didn't work:

e.preventDefault();

return false;

Following is the function that handles the form submission.

export const useCreateAgent = (
  onCompleted: () => void,
) => {
  const [createAgent] = useMutation(CREATE_AGENT, {
    onCompleted,
  });

  return useCallback(async (agent: IAgent) => {
    try {
      await createAgent(createVariables({ ...agent }));
    } catch (errors) {
      return errors;
    }
  }, [createAgent]);
}
Diacid answered 26/12, 2019 at 22:15 Comment(0)
D
12

keepDirtyOnReinitialize prop fixed that issue.

<Form keepDirtyOnReinitialize >
   ...
</Form>
Diacid answered 27/12, 2019 at 1:56 Comment(1)
Thanks lot Ishan Patel. Save me from lot of trouble.Deputation
C
0

If it is just a question about the looks between submission and new page:

event.persist() prevents React from resetting the event object.

Carn answered 26/12, 2019 at 23:42 Comment(1)
That doesn't seem to persist the data. I have tried to use it.Diacid

© 2022 - 2024 — McMap. All rights reserved.