Within my app I get error messages from my api, these are then saved into state const [errors, setErrors] = useState({});
In my view I am rendering my error alert using a ternary operator, but this is leaving some of the alert left in view when closing (because of the '' in the falsey return i suspect). I don't want this of course
My errors object will look something like this {"errors": {"newBookingError": "Something Went Wrong - Please Try Again"}}
{'newBookingError' in errors ? (
<ErrorAlert
errors={errors}
showError={showError}
handleShowErrors={handleShowErrors}
/>
) : (
''
)}
Is there a better way to deal with this?
CodePudding user response:
To avoid the ternary operation you can simply use &&:
{'newBookingError' in errors && (
<ErrorAlert
errors={errors}
showError={showError}
handleShowErrors={handleShowErrors}
/>
)}
This simply evaluates to false if the condition is not met.
You can also use null instead of an empty string as part of the ternary operation.
