How to setValue with react-hook-form in datagrid from materialUi

I am trying to save the values ​​obtained in updatedRows in my given state with react-hook-form to then be able to send it to my function when submitting react-hook-form

what I do :

when a user updates the quantity in my table, then if it is greater than 0, I add it in ‘updatedRows’

if the quantity returns to 0, I remove it from this table

for the moment ‘updatedRows’ works and updates the array according to the quantities added in each row

I would now like to save them in react-hook-form

I thought of doing a ‘useEffect’, and as soon as ‘updatedRows’ changes, then I use ‘setValue’ to save the data of updatedRows in react-hook-form

The worries :

since I put ‘setValue’ in the useEffect, I have a problem with multiple rendering, it is impossible for me to update the quantities…

should I use something other than set Value?

datagrid will become a reusable component, where I pass the columns/rows as parameters.

Sometimes, this reusable datagrid will not need to save values ​​in react-hook-form and will simply be used to display data, which is why I wasn’t thinking of using useFieldArray.

This is my reusable datagrid component :

import { Box, FormLabel } from '@mui/material';
import { DataGrid } from '@mui/x-data-grid';
import React, { useCallback, useState, useEffect } from 'react';
import { useFormContext } from 'react-hook-form';

export const Datagrid = ({ infos }) => {
  const { setValue } = useFormContext();
  const [updatedRows, setUpdatedRows] = useState([]);

  
  const handleProcessRowUpdate = useCallback(
    (newRow, oldRow) => {
      console.log(newRow, oldRow);
       if (newRow.quantity !== oldRow.quantity) {
         setUpdatedRows((prevRows) => {
          const existingIndex = prevRows.findIndex(row => row.id === newRow.id);
          const newRows = [...prevRows];
          if (existingIndex > -1) {
             if (newRow.quantity > 0) {
              newRows[existingIndex] = newRow;
            } else {
               newRows.splice(existingIndex, 1);
            }
          } else if (newRow.quantity > 0) {
             newRows.push(newRow);
          }
          return newRows;
        });
        return newRow;
    
      }
      return oldRow;
    },
    []
  );
  useEffect(() => {
console.log(updatedRows)

 }, [updatedRows]);

 
 
  return (
    <Box>
      <FormLabel>{infos?.inputTitle}</FormLabel>
      {JSON.stringify(updatedRows)}
      <DataGrid
      getRowId={(row) => {
        return row?.id;
    }}
        rows={infos.rows}
        columns={infos.columns}
        processRowUpdate={handleProcessRowUpdate}
        onProcessRowUpdateError={(err) => console.log(err)}
        experimentalFeatures={{ newEditingApi: false }} 
      />
    </Box>
  );
};

export default Datagrid;

And here, my configuration, where i pass the column and rows to datagrid

export const refillSupportContractFormConfiguration = (articles, region) => {
  console.log(region, 'region');
  const updatedArticles = articles.map((article, index) => {
    const priceKey = Object.keys(article?.price).find((key) =>
      key.includes('en')
    );

    const initialPrice = article.price[priceKey];
    console.log(initialPrice);
    return {
      id: article.id || index,
       ...article,
      quantity: 0,
      initialPrice: initialPrice,
    };
  });

  return {
    'S. refill plan': {
      title: 'Support refill plan',
      InputFields: [
        {
          inputType: 'array',
          inputTitle: 'Select the desired Refill plan and the quantity',
          rows: updatedArticles,
          columns: [
            {
              field: 'name',
              headerName: 'Package',
              flex: 1,
            },
            {
              field: 'quantity',
              headerName: 'Quantité',
              type: 'number',
              flex: 1,
              editable: true,
            },
            {
              field: 'initialPrice',
              headerName: 'Initial price',
              type: 'number',
              width: 120,
              valueGetter: (params) =>
                params?.row?.quantity * params?.row?.initialPrice,
              renderCell: (params) =>
                params?.row?.quantity * params?.row?.initialPrice,
            },
          ],
          register: 'supportContract.refill',
        },
      ],
    },
  };
};

It’s the first time since the new MAJ that I am using processRowUpdate from mui and datagrid. Maybe the mistake is from here ?

I also already try to not use ‘useState’ and to directly use setValue in processRowUpdate but I got the same probleme.

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