Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
if user type existing email in the text field, button should be disabled in react js
import React, {
  useState
} from "react";
export default function DropDown() {
  const [mail, useEmail] = useState("");
  const [disabled, useDisabled] = useState(false)
  const handleChange = (e) => {
    useEmail(e.target.value)
  }
  if (mail.includes('s@gmail.com')) {

    React.useEffect(() => {
      setInterval(() => {
        useDisabled(true);
      }, 2000);
    })
  } else {
    useDisabled(false)
  }
  return (
    <>
      <input 
        value={ mail }
        onChange={ handleChange }
        maxLength="20" 
      />
      <button 
        disabled={ mail.length < 1 }
      > Submit </button>
    </>
  );
}

but at the time it throws error like "Too many re-renders. React limits the number of renders to prevent an infinite loop." how to resolve that error?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

TL.DR:

import React, { useState } from "react";
export default function DropDown() {
    const [mail, setEmail] = useState("");

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

    return (
        <>
            <input value={mail} onChange={handleChange} maxLength={20} />
            <button disabled={mail.includes("s@gmail.com") || mail.length < 1}> Submit </button>
        </>
    );
}

Let's break it down:

  1. Hooks (useState, useEffect, etc) cannot be conditionally rendered, so you cannot write them inside if else statements.
  2. const [mail, setMail] = useState(""). The second attribute should be setSomething, not useSomething. The useSomething should be reserved to the declarations of new hooks. The setMail returned by the useState is not a hook, but a normal function (the useState itself is the hook).
  3. setState (or setDisabled, in your example) should never be used directly inside the render of a component. A setState causes the component to re-render so, if when re-rendering a setState is encountered, it will trigger another re-render, that will trigger another setState, and so on (leading to the ""Too many re-renders." you had). Generally speaking, setState should only be used on event handlers (such as onClick, onKeyDown, onSomethingElseHapped, etc)

There are a few more minor issues with your code. I encourage you to go through the basics of react one more time.

about 4 years ago · Juan Pablo Isaza Relatório

0

You won't need to create a state for the disabled. You can simply validate using it on every render. The sample is given below. Whenever you update input, the component will retrigger and validation will be performed.

import { useState } from "react";
import "./styles.css";
const blackListed = /^s@gmail\.com$/;
const emailReg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

const isEmailValid = (email = "") => {
  if (!email.length) return false;
  if (!emailReg.test(email)){
    return false
  }
  return !blackListed.test(email);
};
export default function App() {
  const [mail, setEmail] = useState("");
  const handleChange = ({ target: { value } }) => {
    setEmail(value);
  };
  const isValidEmail = isEmailValid(mail);
  return (
    <div>
      <input value={mail} onChange={handleChange} maxLength="20" />
      <button disabled={!isValidEmail}> Submit </button>
    </div>
  );
}

If you want to clear email after a certain time. You can use useEffect with setTimeout and watch for change.

 useEffect(() => {
    const id = setTimeout(() => {
      if (!isValidEmail && mail.length) {
        alert("Your email is invalid");
        setEmail("")
      }
    }, 1000)
    return () => clearTimeout(id);
  }, [isValidEmail])

Complete code:

import { useState, useEffect } from "react";
import "./styles.css";
const blackListed = /^s@gmail\.com$/;
const emailReg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

const isEmailValid = (email = "") => {
  if (!email.length) return false;
  if (!emailReg.test(email)) {
    return false
  }
  return !blackListed.test(email);
};


export default function App() {
  const [mail, setEmail] = useState("");
  const handleChange = ({ target: { value } }) => {
    setEmail(value);
  };
  const isValidEmail = isEmailValid(mail);

  // Clear after some toat and message.
  useEffect(() => {
    const id = setTimeout(() => {
      if (!isValidEmail && mail.length) {
        alert("Your email is invalid");
        setEmail("")
      }
    }, 1000)
    return () => clearTimeout(id);
  }, [isValidEmail])

  return (
    <div>
      <input value={mail} onChange={handleChange} maxLength="20" />
      <button disabled={!isValidEmail}> Submit </button>
    </div>
  );
}

Working code:

https://codesandbox.io/embed/nervous-cache-tm5yt?fontsize=14&hidenavigation=1&theme=dark

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda