How to Prevent Excessive Re-renders in Dynamic Forms Using React Hook Form and Material-UI?

I’m developing a dynamic form with React Hook Form and Material-UI where the content changes based on the first two user selections. My setup includes a “Generator” page that renders different form configurations based on these selections. However, I’m encountering performance issues as the entire Generator component re-renders whenever an input value changes.

Here’s a simplified outline of my setup:

  • formConfiguration: Always visible and does not change.
  • combinedConfigurations: Changes based on initial user choices.
    Generator: Renders forms based on the configuration.
    Each input modification triggers a re-render of the Generator, as indicated by a console.log(‘generator’) call.

I have attempted using useMemo and useCallback to optimize performance but the issue persists.

Here’s my generator page

//all my import...

export const Generator = () => {
  const dispatch = useDispatch();
  const methods = useForm({
    defaultValues: { 
      createdBy: 'User one',
    },
  });

  const step = useSelector((state) => state.step.actual);
  const customers = useSelector((state) => state.customer.customers);
 
  const customerUsers = useSelector((state) => state.customer?.contacts);
  const categories = useSelector((state) => state.categories?.categories);
  const services = useSelector((state) => state.services?.services);
  const regions = useSelector((state) => state.regions?.regions);
  const stacks = useSelector((state) => state.stacks?.stacks);
  const profiles = useSelector((state) => state.profiles?.profiles);
  const articles = useSelector((state) => state.articles?.articles);


  const category = methods.watch('category');
  const service = methods.watch('service');
  const region = methods.watch('region');
  const customer = methods.watch('customer');

  const mergeFormConfigurations = (baseConfig, additionalConfig) => {
    return { ...baseConfig, ...additionalConfig };
  };

  const getFormConfiguration = () => {
    let combinedConfigurations = formConfiguration(
      category,
      customers, 
      categories,
      services,
      customerUsers,
      regions
    );

    if (service?.name === 'Consultancy') {
      combinedConfigurations = mergeFormConfigurations(
        combinedConfigurations,
        consultancyFormConfiguration(stacks, profiles)
      );
    }
    if (service?.name === 'New support contract') {
      combinedConfigurations = mergeFormConfigurations(
        combinedConfigurations,
        newSupportContractFormConfiguration(articles)
      );
    }
    if (service?.name === 'Refill support contract') {
      combinedConfigurations = mergeFormConfigurations(
        combinedConfigurations,
        refillSupportContractFormConfiguration(articles, region)
      );
    }
    return combinedConfigurations;
  };

  const currentFormConfiguration = getFormConfiguration();

  const FormWrapper = ({ step, methods }) => {
    console.log('FormWrapper');

    const configuration = currentFormConfiguration[step];
    if (!configuration) {
      return null;
    }

    const onSubmit = (data) => {
      const newData = { ...data, articles };
      postOffer(newData);
    };

    const test = methods.watch();
    return (
      <Box>
        <form onSubmit={methods.handleSubmit(onSubmit)}>
          <NewOffer />
          <Save />

          <FormComponent
            title={configuration.title}
            InputFields={configuration.InputFields}
          />
        </form>
      </Box>
    );
  };

  useEffect(() => {
    dispatch(setLocation('generator'));
     dispatch(getCategories());
    dispatch(getRegions());
  }, []);

  useEffect(() => {
    region && dispatch(getCustomers(1, region));
  }, [region]);

  useEffect(() => {
    category && dispatch(getServices(category?.category_id));
  }, [category]);

  useEffect(() => {
    customer && dispatch(getCustomerContacts(customer.customerId));
  }, [customer]);

  useEffect(() => {
    if (service)
      switch (service.name) {
        case 'Consultancy': {
          dispatch(getStacks());
          dispatch(getProfiles());
          break;
        }
        case 'New support contract': {
          dispatch(getArticlesByService('support', 'new'));
          break;
        }
        case 'Refill support contract': {
          dispatch(getArticlesByService('support', 'refill'));
          break;
        }
        default:
          break;
      }
  }, [service, region]);

  console.log('generator');
  return (
    <Box>
      <FormProvider {...methods}>
        <StepsBar />
        <FormWrapper step={step} methods={methods} />
      </FormProvider>
    </Box>
  );
};

Then a Form configuration exemple :

export const formConfiguration = (
  categoryChoice,
  customers, 
  categories,
  services,
  customerUsers,
  regions
) => {
  return {
    Category: {
      title: 'Category',
      InputFields: [
        {
          pieTitle: 'Category',
          inputType: 'pie',
          register: 'category',
          Size: 500,
          datas: categories,
        },
      ],
    },
    Services: {
      title: 'Services',
      InputFields: [
        {
          pieTitle: 'Services catalog',
          inputType: 'pie',
          register: 'service',
          Size: 500,
          datas: services,
        },
      ],
    },
    'Informations générales': {
      title: 'Informations générales',
      InputFields: [
        {
          inputType: 'radio',
          inputTitle: 'Sélectionnez la region concernée',
          options: regions,
          optionLabel: 'code',
          optionValue: 'code',
          register: 'region',
        },
        {
          inputType: 'mask',
          inputLabel: 'N° DEVIS',
          inputTitle: 'Numéro de devis',
          mask: 'DEV-99-9999',
          register: 'dev',
          defaultValue: 'DEV-XX-XXXX',
        },
        {
          inputType: 'text',
          inputLabel: 'Référence',
          inputTitle: 'Votre référence',
          register: 'reference',
          type: 'text',
        }
      ],
    },
  };
};

then I use the fields configuration here

export const FormComponent = ({ title, InputFields }) => {
  console.log(InputFields, 'FormComponent');
  return (
    <Box>
      <Typography variant="h4">{title}</Typography>

      {InputFields?.map((el) => (
        <InputField infos={el} />
      ))}
    </Box>
  );
};

The Input switch

//all import

export const InputField = ({ infos }) => {
  switch (infos?.inputType) {
    case 'radio':
      return <Radio infos={infos} />;
    case 'text':
      return <Text infos={infos} />;
    case 'mask':
      return <Mask infos={infos} />;
    case 'pie':
      return <PieChart infos={infos} />;
    default:
      return null;
  }
};

and then a input

export const Text = ({ infos }) => {
  const { register } = useFormContext();

  return (
    <Box sx={{ display: 'flex', flexDirection: 'column' }}>
      <FormLabel>{infos?.inputTitle}</FormLabel>
      <TextField
        label={infos?.inputLabel}
        type={infos?.type}
        {...register(infos?.register, infos?.registerParams)}
      />
    </Box>
  );
};

Could using useMemo or useCallback be the right approach to prevent these re-renders, or should I consider restructuring my components? Any suggestions on how to improve the structure or the use of hooks in this scenario would be greatly appreciated.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật