简体   繁体   English

用户提交表单后,如何防止 React-Final-Form 重置表单值?

[英]How to prevent React-Final-Form to reset the form values after user submits the form?

I am using react-final-form in React App.我在 React App 中使用react-final-form 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.但是有什么方法可以防止react-final-form form 在用户成功提交表单后重置react-final-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.我需要它的原因是,目前当用户在提交表单后被重定向时,在提交表单和重定向到其他组件的操作之间,用户看到输入值被重置为初始表单值200-400 micro second

I tried using following but didn't work:我尝试使用以下但没有奏效:

e.preventDefault(); 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]);
}

keepDirtyOnReinitialize prop fixed that issue. keepDirtyOnReinitialize道具修复了这个问题。

<Form keepDirtyOnReinitialize >
   ...
</Form>

If it is just a question about the looks between submission and new page:如果只是关于提交和新页面之间外观的问题:

event.persist() prevents React from resetting the event object. event.persist() 阻止 React 重置事件对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM