简体   繁体   English

React-hook 表单从 isSubmitting 转换为 isSubmitted state 需要 5 多秒。 如何减少这种延迟以提高性能?

[英]React-hook form is taking 5+ seconds to shift from isSubmitting to isSubmitted state. How can I reduce this lag to improve performance?

I have written a form which has more than 30 fields(material-ui components) using react-hook form.我使用 react-hook 表单编写了一个包含 30 多个字段(material-ui 组件)的表单。 All of the fields have different kinds of validation depending upon the type of the field.根据字段的类型,所有字段都有不同类型的验证。

The sample folder structure is as shown in this link.示例文件夹结构如该链接所示。 folderStructure文件夹结构

detail_page.js is the root component. detail_page.js是根组件。 In this component useForm is initialized with defaultValues.在这个组件中,useForm 使用 defaultValues 进行初始化。 And the form is wrapped with FormProvider(Context to pass to the deeply nested components).并且表单用 FormProvider 包装(传递给深层嵌套组件的上下文)。 Also handleSave method is written in this component only. handleSave 方法也只写在这个组件中。 Upon clicking SAVE button, handleSubmit(handleSave) is called.单击 SAVE 按钮后,将调用handleSubmit(handleSave) From this component TopLevel(top_level.js) component is called.从此组件调用TopLevel(top_level.js)组件。

top_level.js file groups fields(passed as props from detail_page.js) into sections. top_level.js文件将字段(从 detail_page.js 作为道具传递)分组。 A render method( renderSections ) is written to display each section in mui-accordion, and all the fields within that section is passed to iterator.js file.编写了一个渲染方法( renderSections )来显示 mui-accordion 中的每个部分,并且该部分中的所有字段都传递给iterator.js文件。 the renderSections method is memoized with the dependency array of sectionobjects. renderSections方法使用 sectionobjects 的依赖数组进行记忆。

iterator.js file loops through each of the the fields passed as props from top_level.js and calls respective system_component based on the type of the field (Radio,checkbox,textbox,textarea,email,password,file_uploader,address,latlong,phone_number,list,date,date_range etc). iterator.js文件循环遍历从 top_level.js 作为 props 传递的每个字段,并根据字段的类型调用相应的system_component (Radio,checkbox,textbox,textarea,email,password,file_uploader,address,latlong,phone_number,列表、日期、日期范围等)。

system_component is the actual component which wraps the material-ui's component using useController/Controller. system_component是使用 useController/Controller 包装 material-ui 组件的实际组件。 Based on the type of field, respective system_component is called in iterator.根据字段的类型,在迭代器中调用相应的 system_component。 for eg:-例如:-

  • SystemTextBox for type input SystemTextBox 用于类型输入
  • SystemEmail for type email in input输入中 email 类型的 SystemEmail
  • SystemDate for type date and so on. SystemDate 用于类型日期等。

Some system component may be a combination of 2 or more mui's components.某些系统组件可能是 2 个或更多 mui 组件的组合。 for eg -SystemLatlong- combination of 4 or more text inputs and number input to display address components of the particular latlong.例如 -SystemLatlong- 组合 4 个或更多文本输入和数字输入以显示特定 latlong 的地址组件。

  • SystemArray - any combination of rest of the system components(calling iterator again for this purpose) and so on. SystemArray - 系统组件的 rest 的任意组合(为此再次调用迭代器)等等。

Now the problem is, whenever I click on save button, the save button freezes, In handleSubmit method I am setting MuiBackdrop component to display "Saving" text.现在的问题是,每当我单击保存按钮时,保存按钮都会冻结,在handleSubmit方法中,我将 MuiBackdrop 组件设置为显示“保存”文本。 This backdrop is called after 5+ seconds upon clicking save button.单击保存按钮 5 秒以上后调用此背景。 In detail page I have consoled isSubmitting, isValidating, isSubmitted state of formState.详细页面我已经安慰了formState的isSubmitting,isValidating,isSubmitted state。 I figured out that, for almost 5+ seconds isSubmitting state holds true value, until which handleSave function is not triggered.我发现,提交 state 几乎 5 秒以上的时间保持真实值,直到不会触发 handleSave function。

the reason for this is that react-hook-form uses context which is not optimized for performant re-renders.这样做的原因是 react-hook-form 使用了未针对高性能重新渲染进行优化的上下文。 With context all consumers of that context will re-render regardless of if they care of the value that changed.使用上下文,该上下文的所有消费者都将重新呈现,无论他们是否关心已更改的值。 With react hook form having one central context every input re-renders when isSubmitting is changed unfortunately.使用具有一个中央上下文的反应钩子形式,不幸的是,当isSubmitting发生更改时,每个输入都会重新呈现。

I'm building a library that will solve this issues that is currently in beta called @zerry/react-formz .我正在构建一个库来解决这个目前处于测试阶段的问题,称为@zerry/react-formz It guarantees isolated re-renders for inputs which means near constant performance no matter how many inputs you are rendering.它保证了输入的隔离重新渲染,这意味着无论您渲染多少输入,性能都接近恒定。

Its' in beta and currently being worked on but it might solve the issues you are having.它处于测试阶段,目前正在开发中,但它可能会解决您遇到的问题。

You can check it out here if you feel so inclined: https://react-formz.zerry.dev/docs/performance/如果您愿意,可以在这里查看: https://react-formz.zerry.dev/docs/performance/

import {
  Form,
  TextField,
  NumberField,
  DependentTextField,
} from "@zerry/react-formz";

const NestedInput = () => {
  return (
    <TextField
        required
        name="name"
        as={({ input }) => <input {...input} />}
    />
  )
}

const MyForm = () => {
  return (
    <Form initialValues={{ name: "", age: "" }}>
      <NestedInput />
      <NumberField
        required
        name="age"
        as={({ input }) => <input {...input} />}
      />
    </Form>
  );
};

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

相关问题 如何使用react-hook按时更新state - How to update state on time with react-hook 带有 useFieldArray 的 React-Hook Form:TextField 值未显示在控制台中。 我该如何解决? - React-Hook Form with useFieldArray: TextField value does not show up in the console. How can I fix it? 使用 react-hook 调用 setState 后如何使用更新的状态 - How to use the updated state after setState call with react-hook React-Hook Form:如何将数量的值转换为 useFieldArray 内的数字? - React-Hook Form: How do I convert the value of the quantity into a number that is inside the useFieldArray? 如何在setState(react-hook)中合并…对象和函数调用的返回值? - How can I merge …object and return value of function call in setState(react-hook)? react-hook useFormInput() 表单验证不起作用 - react-hook useFormInput() form validation not working react-hook 组件形式中的 initialValues - initialValues in react-hook compoenent Form 带有 SliderComponent 输入的 react-hook -form - react-hook -form with SliderComponent input 反应:我有一个 api fetch 并且数据存储到我的钩子状态。 为什么我可以 console.log 钩子对象,但不能访问它? - React: I have an api fetch and the data stores into my hook state. Why can I console.log the hook object, but not access it? Formik表单正在提交时,如何向按钮添加类? - How can I add a class to a button when a Formik Form isSubmitting?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM