0

Using React functional component, I'm trying to validate the email address. Below is my code.

Problem is when I tried to enter any text nothing happens, its not allowing any letters to be entered.

Below is my component code, Please help me out

const UpdateEmailAddress: React.FC<Step> = (props: Step) => {
  const [customerEmail, setCustomerEmail] = useState<any>('');
  const [checkValidEmail, setcheckValidEmail] = useState<boolean | null>(null);
    
  const emailValidate = (value: any) => {
    if (!validEmailRegex(value)) {
      setcheckValidEmail(true);
    } else {
      setcheckValidEmail(false);
    }
  };

  const handleChange = (e: any) => {
    if (e.target.value) {
      setCustomerEmail(e.target.value);
    }
  };

  return (
    <>
      <div className="step container update-email-container pt-10" ref={props.domRef}>
        <div className="row">
          <div className="col-md-4">
            <div className="mb-4">
              <TextField
                aria-label={content.emailAddress.header}
                enableClearText
                errormessage={content.emailAddress.emailError}
                helptext={content.emailAddress.helptext}
                id="emailAddress"
                isValid={checkValidEmail}
                label={content.emailAddress.emailAddress}
                maxLength={0}
                name="emailAddress"
                tooltipText=""
                type="email"
                onChange={(e:any) => handleChange(e)}
                value={customerEmail}
              />
            </div>

            <ButtonAction
              onClick={emailValidate(customerEmail)}
              className="load-more-button mb-0"
              type="button"
              aria-label={content.emailAddress.nextButton}
            >
              {content.emailAddress.nextButton}
            </ButtonAction>
          </div>
        </div>
      </div>
    </>
  );
};

2 Answers 2

1

Your logic in onChange method is wrong it should be

onChange={(e: React.ChangeEvent<HTMLInputElement>) => setCustomerEmail(e.target.value)}
Sign up to request clarification or add additional context in comments.

7 Comments

Uncaught TypeError: Cannot read properties of undefined (reading 'value') I get this error
can you share your component on codesandbox.io
I've added the entire component code, check if that helps
above code should work. Are you using material ui ?
No, We're using built in components
|
0

You have to define the onChange() method.

It should be like this

const handleChange = (e) => {
    setCustomerEmail(e.target.value);
  };

Then you will be able to enter textfield.

Secondly, you have to pass the value in emailvalidate() in onClick() of button.

<ButtonAction onClick={()=>emailValidate(customerEmail)} className="load-more-button mb-0" type="button" aria-label={content.emailAddress.nextButton}>
              {content.emailAddress.nextButton}
            </ButtonAction>

And remove e parameter in emailValidate(value) as it is only using value and not e.

6 Comments

Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. now getting this error
@ArunKumar onChange={(e) => handleChange(e)} this shall remove the re-render issue
No it didn't, I was already using like that only, issue is still coming
Try this onClick={emailValidate(customerEmail)}
@mohitmaroliya already onClick={emailValidate(customerEmail)} is added to button component
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.