I have an object which comes from FieldErrors
react-hook-form, however, I am not sure how to access the object in order to retrieve the message
property. The object shown in the image below comes from react-hook FieldErrors
where I added the following console statement:
console.log('this1: ', errorProps)
I am not sure how to access this errorProps
message property. FieldErrors
has message, type, root, ref
and types
associated with it however all of them return back undefined for example, console.log('this1: ', errorProps?.message)
returns back undefined. How can I access the message property shown in the image?
<Box display="flex" flexDirection="column" >
{Object.entries(errors).map(([fieldId, errorProps]) => (
<Box display="flex" >
{console.log('this1: ', errorProps)}
<Typography type="negative">{`${errorProps?.message || errorProps?.root?.message}`}</Typography>
</Box>
))}
</Box>
3
Answers
As @deceze already mentioned in his comment: in order to extract the error message from each of the error-objects you might find in your
errorProps
array you could do the following:The provided code snippet appears to be written in JSX, likely within a React component. It iterates through the entries of an
errors
object, presumably containing error information related to form fields. For each entry, it attempts to render an error message using theTypography
component. However, there is a potential issue with usingconsole.log
within JSX, as JSX is primarily intended for rendering components.