I have a MaterialUI Textfield for email which validates input when the user leaves the field. I make use of a function on onBlur for that purpose. I want to show an error message using helpertext.
The code for email field:
<TextField
margin="dense"
id="emailAddress"
name="email"
label="email"
type="email"
onChange={onChange}
onBlur={validateEmail}
value={email}
error={validEmail}
helperText={validEmail ? 'Please enter a valid Email' : ' '}
fullWidth
/>
The validEmail variable is initialized inside the functional component as follows:
let validEmail = false;
And validateEmail is defined inside the same component as:
const validateEmail = () => {
if (!email || invalidEmail(email)) {
validEmail = true;
} else {
validEmail = false;
}
};
But it doesn't work and error message doesn't get displayed. What am I potentially missing here? The condition that checks invalidEmail does get executed.