I have a React component with an errors: {} object in the state. This object essentially mirrors the user: {} object, which gets data from a form in the component.
The error object will store any error messages that are returned from attempting to save to the data store.
For example
If the person forgets to enter field username, the response will contain an "errors" object. The actual error message will be in: errors.username.message
The component to display this error message should display if the message exists. I pass the potential error message through a prop errorText:
<Input value={ user.username } name="username" errorText={ errors.username }
placeholder="username"
displayName="Database Username" type="text" onChange={ updateFormField } />
This is working, whether errors.username is actually a key in the object or not.
However, I am getting an error in my Input component because it can't find message of undefined.
I thought I could test for the key using in but was incredibly wrong about this. Here is my Input component:
export const Input = (props) => (
<div className="form-group">
<label htmlFor={ props.name }>{ props.displayName }</label>
<input type={ props.type } value={ props.value } placeholder={ props.placeholder }
name={props.name} id={props.name} onChange={ props.onChange } className="form-control"/>
{ "errorText" in props ? "message" in props.errorText ? (
<label className="label label-danger">{ props.errorText.message }</label>
) : "" : (
""
)}
</div>
);
Any suggestions?