Improving Formik Performance when it's Slow (Material UI) by@josejaviasilis

Improving Formik Performance when it's Slow (Material UI)

Jose Javi Asilis HackerNoon profile picture

Jose Javi Asilis

Excellent and getting better 🔥🔥🔥! Focused on building tech startups to shape the world and solve big problems 🚀.

Material UI V5 Landing Page (Taken from and Formik’s landing page (Taken from

This will show you an alternative way (Compared to Matt Faircliff and Julian Rachmann) in which you can increase Formik’s performance when you have multiple inputs and typing into an input just becomes unbearable. The cause of this problem is CSS-In-JS and the constant re-rendering of Emotion as Formik re-renders on every keystroke.

This method isn’t necessarily exclusive to Material-UI (Although MUI is being used as an example here, and this works for both: V4.x and V5.x) and can be used with any other UI library, as the concepts are the same. Beware, this is hacky, but works in production.

Here’s the GitHub repo that showcases you how to use it.

The Concept - TL;DR

The concept is pretty simple: We let the <TextField /> component handle its own internal state, and then we propagate the change to Formik with an onBlur event. We also add additional safeguards to allow for pre-filled passwords to work. In addition, when you dynamically change an attribute of the TextFieldsuch as name,we need to have the correct way to propagate it correctly as the onBlur won’t fire. Or when you change <Formik initialValues> while having enableReinitialize={true}.

The solution

Wrap your TextField component with the following code (Uses TypeScript):


import { TextFieldProps, TextField } from "@mui/material";
import { useField } from "formik";
import React, { memo, useEffect, useState } from "react";
import { usePropagateRef } from "./usePropagateRef";
export type PerformantTextFieldProps = Omit<TextFieldProps, "name"> & {
  name: string;
   * IF true, it will use the traditional method for disabling performance
  disablePerformance?: boolean;
  loading?: boolean;
  min?: number;
  max?: number;
 * This is kind of hacky solution, but it mostly works. Your mileage may vary
export const PerformantTextField: React.FC<PerformantTextFieldProps> = memo(
  (props) => {
    const [field, meta] = useField(;
    const error = !!meta.error && meta.touched;
     * For performance reasons (possible due to CSS in JS issues), heavy views
     * affect re-renders (Formik changes state in every re-render), bringing keyboard
     * input to its knees. To control this, we create a setState that handles the field's inner
     * (otherwise you wouldn't be able to type) and then propagate the change to Formik onBlur and
     * onFocus.
    const [fieldValue, setFieldValue] = useState<string | number>(field.value);
    const { disablePerformance, loading, ...otherProps } = props;
      value: field.value,
     * Using this useEffect guarantees us that pre-filled forms
     * such as passwords work.
    useEffect(() => {
      if (meta.touched) {
if (field.value !== fieldValue) {
      // eslint-disable-next-line
    }, [field.value]);
const onChange = (evt: React.ChangeEvent<HTMLInputElement>) => {
    const onBlur = (evt: React.FocusEvent<HTMLInputElement>) => {
      const val = || "";
      window.setTimeout(() => {
          target: {
            value: props.type === "number" ? parseInt(val, 10) : val,
      }, 0);
// Will set depending on the performance props
    const performanceProps = disablePerformance
      ? {
          value: loading ? "Loading..." : fieldValue,
      : {
          value: loading ? "Loading..." : fieldValue,
          onFocus: onBlur,
return (
            ...((props.type === "number" && {
              inputProps: { min: props?.min, max: props?.max },
            }) ||
          helperText={meta.touched && meta.error}

We add a helper hook called usePropagateRef to allow us overcome an edge case when the name attribute of the TextField changes and data needs to be changed.


import { useEffect, useRef } from 'react';
type UsePropagateRefProps = {
  setFieldValue: React.Dispatch<React.SetStateAction<any>>;
  name: string;
  value: any;
export function usePropagateRef(props: UsePropagateRefProps) {
  const { name, value, setFieldValue } = props;
   * This is a special useRef that is used to propagate Formik's changes
   * to the component (the other way around that is done).
   * This needs to be done whenever the name property changes and the content of the
   * component remains the same.
   * An example is when you have a dynamic view that changes the TextField's name attribute. 
   * If we don't do this, the useBlur hook will overwrite the value that you left before you 
   * changed the TextField's value. 
  const flagRef = useRef(true);
  useEffect(() => {
    if (flagRef.current) {
      flagRef.current = false;
    // eslint-disable-next-line
  }, [name]);

This should set you to a much more smooth experience while using the <TextField />

GitHub Repo

Check it out here:

The files to be checked can be found here: src/components/Fields/Form/PerformantTextField/

Jose Javi Asilis HackerNoon profile picture
by Jose Javi Asilis @josejaviasilis.Excellent and getting better 🔥🔥🔥! Focused on building tech startups to shape the world and solve big problems 🚀.
Read my stories