import { useCallback, useMemo } from 'react'; import { useAppDispatch } from…
- User
import { useCallback, useMemo } from 'react';
import { useAppDispatch } from '@/shared/hooks';
import { popupsActions } from '../../model'; import { POPUPS } from '../../config';
export function useAddCustomer() { const dispatch = useAppDispatch();
const addCustomerPopup = useMemo( () => ({ name: POPUPS.addCustomer, onClose: () => dispatch(popupsActions.close(addCustomerPopup)), }), [dispatch] );
return useCallback( () => dispatch(popupsActions.open(addCustomerPopup)), [dispatch, addCustomerPopup] ); }
ient.js:1 A non-serializable value was detected in an action, in the path:
payload.onClose. Value: ()=>dispatch(model__WEBPACK_IMPORTED_MODULE_2_.popupsActions.close(addCustomerPopup)) Take a look at the logic that dispatched this action: {type: 'popups/close', payload: {…}}payload: {name: 'addCustomer', onClose: ƒ}type: "popups/close"[[Prototype]]: Object (See https://redux.js.org/faq/actions#why-should-type-be-a-string-or-at-least-serializable-why-should-my-action-types-be-constants) (To allow non-serializable values see: https://redux-toolkit.js.org/usage/usage-guide#working-with-non-serializable-data) c - Assistant
This warning message is indicating that a non-serializable value has been detected in the
onCloseproperty of an action payload. Specifically, the value is a function which is not serializable.To resolve the warning, the function should be removed from the payload and handled separately. One way to do this is to define the function outside of the 'useMemo' hook and pass it as a parameter to the 'useMemo' hook:
const handleClose = () => dispatch(popupsActions.close(addCustomerPopup)); const addCustomerPopup = useMemo( () => ({ name: POPUPS.addCustomer, onClose: handleClose, }), [dispatch] );Alternatively, instead of storing the
addCustomerPopupobject in a memoized variable, theonClosefunction can be defined inside theuseCallbackhook:return useCallback( () => dispatch(popupsActions.open({ name: POPUPS.addCustomer, onClose: () => dispatch(popupsActions.close({ name: POPUPS.addCustomer })), })), [dispatch] );This way, the function is only created when the
useCallbackhook is called, and no non-serializable value is stored in the action payload. - User
- Assistant
This error is occurring because you're passing
"invoice_emails"as a string to theregisterfunction in this line:{...register(`invoice_emails.${i}` as const)}The
registerfunction expects a key path, which is an array of keys, not a string. The key path is used to map the value of the input field to the appropriate property of the form data object.To fix the error, you should pass an array of keys instead of a string. You can do this by replacing
invoice_emails.${i}with["invoice_emails", i]:{...register(["invoice_emails", i] as const)}This will pass an array with two elements - the first is the key
"invoice_emails", and the second is the indexi.You also need to update the
yupschema to useyup.array().of(...)instead ofyup.string()to validate the items in the"invoice_emails"array:const schema = yup.object().shape({ email: yup.string().required("Введите Email"), invoice_emails: yup.array().of( yup .string() .email("Email должен быть валидным") .required("Введите Email") ), }); - User
- Assistant
The error message is indicating that the
nameproperty in theregisterfunction is of typenever. This occurs when an invalid key path is passed to theregisterfunction.Based on your schema, the
invoice_emailsproperty is an array of objects, not an array of strings. Therefore, to register eachinvoice_emailsitem, you need to use the formatinvoice_emails[index].property, whereindexis the index of the item in the array, andpropertyis the name of the property that you want to register.In this case, the property is
name, so you need to update theregisterfunction to use the correct key path:{...register(`invoice_emails.${i}.name` as const)}This indicates that you want to register the
nameproperty of thei-th item in theinvoice_emailsarray.Also, in the schema, you need to change the validation of the property to an array of objects with a property, like this: