so I'm having some issues in my code which I'd like to get help with.
as you can see the error alert box is showing although it is supposed to be empty. I ran some code "tests" to see what is happening and rendered it on the page.
consts -
const [displayMessage, setDisplayMessage] = useState({});
const [errorMessage, setErrorMessage] = useState({
username: "",
email: "",
password: "",
});
useEffects -
useEffect(() => {
if (success) {
setMessage(`${data.userName} has been created successfully`);
}
}, [data.userName, success]);
useEffect(() => {
if (typeof errorMessage === "string") {
setDisplayMessage(errorMessage);
} else {
setDisplayMessage(
Object.keys(errorMessage).map((key) => {
return errorMessage[key];
})
);
}
}, [errorMessage]);
ternary operators -
{typeof displayMessage} // "test"
<br />
{displayMessage.length} // "test"
{Object.keys(displayMessage).map((key) => {
console.log(displayMessage[key]);
})}
{displayMessage &&
(typeof displayMessage === "string" ? (
<Alert sx={{ mb: 4 }} severity="error">
{displayMessage}
</Alert>
) : (
<Alert sx={{ mb: 4 }} severity="error">
{Object.keys(displayMessage).map((key, index) => {
return <div key={index}>{displayMessage[key]}</div>;
})}
</Alert>
))}
now as you can see here it's working the way I want it -
my only problem is that the alert box shows empty.

