import { useMemo } from "react"; import Button from "@mui/material/Button"; import {…
- User
import { useMemo } from "react"; import Button from "@mui/material/Button"; import { SubmitHandler, useForm } from "react-hook-form"; import { yupResolver } from "@hookform/resolvers/yup";
import { typedMemo } from "@/shared/hocs"; import { Form } from "@/shared/ui/form-component";
import { ClientDetails } from "../client-details"; import { Organization } from "../organization"; import { BankAccounts } from "../bank-accounts"; import { InvoiceEmails } from "../invoice-emails/ui"; import { Metadata } from "../metadata";
import { FormData, schema } from "./validation"; import "./style.scss";
interface IProps { onSubmit: SubmitHandler; }
export const AddCustomerForm: React.FC = typedMemo(({ onSubmit }) => { const { control, formState: { errors }, handleSubmit, register, setValue, getValues, } = useForm({ resolver: yupResolver(schema), });
console.log("Form: ", errors.invoice_emails);
const deps = JSON.stringify(errors.invoice_emails);
const err = { clientDetails: useMemo( () => ({ name: errors.name, email: errors.email, deferral_days: errors.deferral_days, credit_limit: errors.credit_limit, }), [errors.name, errors.email, errors.deferral_days, errors.credit_limit] ),
code// для того что-бы результат валидации показывать пользователю сразу при вводе значения необходимо явно указать в deps все зависимоти и обновить объект ошибки organization: useMemo( () => ({ organization: errors.organization, }), // eslint-disable-next-line react-hooks/exhaustive-deps [ errors.organization?.addr, errors.organization?.inn, errors.organization?.kpp, errors.organization?.name, errors.organization?.ogrn, ] ), invoiceEmails: useMemo( () => ({ invoice_emails: errors.invoice_emails, }), // eslint-disable-next-line react-hooks/exhaustive-deps [deps] ), bankAccounts: useMemo( () => ({ bank_accounts: errors.bank_accounts, }), [errors.bank_accounts] ),};
return (
code - Assistant
This error occurs because one of the values in the
errors.invoice_emailsobject is a DOM element, specifically an HTMLInputElement. DOM elements cannot be converted to JSON, so theJSON.stringifycall in theuseMemohook fails with a circular structure error.To fix this, you should exclude any DOM elements from the
errors.invoice_emailsobject before callingJSON.stringify. One way to do this is to use a library likelodashto deep clone the object and remove any DOM elements:codeimport { cloneDeep, omitBy, isElement } from "lodash"; // ... const formErrors = cloneDeep(errors.invoice_emails); const filteredErrors = omitBy(formErrors, isElement); const deps = JSON.stringify(filteredErrors);This code clones the
errors.invoice_emailsobject, filters out any DOM elements using theomitByfunction fromlodash, and then converts the filtered object to a JSON string. This should prevent the circular structure error.