Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When NOT to use Formik's FastField?

In the docs of Formik's FastField it have alot of when you should use it and how it works, but is there a case where you shouldn't use?

Because if in somecases FastField is faster and other cases it doesn't make any difference, why not always use FastField? Is there a case where using Field is better than FastField?

like image 565
Vencovsky Avatar asked Jan 21 '20 12:01

Vencovsky


People also ask

What is FastField in Formik?

<FastField /> is meant for performance optimization. However, you really do not need to use it until you do. Only proceed if you are familiar with how React's shouldComponentUpdate() works.

Why is Formik slow?

Typing in a field causes a synchronous rerender on everything that is receiving the value, which means in large applications typing in the fields feels slow and jerky. This has been the plague of the formik model and one big reason why react hook form etc / uncontrolled form libs became popular.


1 Answers

FastField has a shouldComponentUpdate which is 'concerned' about changes to only its own props.

If, your use-case, requires field to re-render because of any other change then don't go for FastField. Even if some other prop change, your component won't be updated.

Also, as per the docs, performance issues due to Formik Field's re-render might surface only on cases where the form is huge (>30 Fields). They recommend using FastFields for >30 Fields only.

shouldComponentUpdate(props: FastFieldInnerProps<Values, Props>) {
    if (this.props.shouldUpdate) {
      return this.props.shouldUpdate(props, this.props);
    } else if (
      props.name !== this.props.name ||
      getIn(props.formik.values, this.props.name) !==
        getIn(this.props.formik.values, this.props.name) ||
      getIn(props.formik.errors, this.props.name) !==
        getIn(this.props.formik.errors, this.props.name) ||
      getIn(props.formik.touched, this.props.name) !==
        getIn(this.props.formik.touched, this.props.name) ||
      Object.keys(this.props).length !== Object.keys(props).length ||
      props.formik.isSubmitting !== this.props.formik.isSubmitting
    ) {
      return true;
    } else {
      return false;
    }
  }
like image 192
Tabish Javed Avatar answered Sep 29 '22 18:09

Tabish Javed