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

140
Visualizações
Too many re-renders. React limits the number of renders ? Socket.io Chat App

I am making a simple real-time chat web-app using socket.io and in the frontend part I am getting Too many render error , even though all my setState are outside the return part of Component I have attached the code for App.js of React frontend part

Pardon me for naive attitude , I am a novice programmer and this is my first question

function App() {
  const [name, setName] = useState("");
  const [msg, setMsg] = useState("");
  const [chat, setChat] = useState([]);

  useEffect(() => {

    socket.on("connect", () => {
      console.log( `Connected`);
    });

    socket.on("message", (msg_obj) => {
      ChatList(msg_obj);
    });
    
  }, []);

  function ChatList(msg_obj){
    setChat((chat) => [...chat, msg_obj]);
  }

  function addChat () 
  {
    const id = socket.id;
    socket.emit("message", { id, msg, name });
    setMsg("");
  };

  function settingname(tobenamed)
  {
    setName(tobenamed);
  }

  function settingmessage(messagevalue)
  {
    setMsg(messagevalue);
  }

  return (
    <div className="App">
      <header className="App-header">

        <div className="chat-area">
          {chat.map((msg_obj) => {
            return (
            <span className="Block">
              <h4 key={msg_obj.id}>{msg_obj.name}</h4>
              <p> {msg_obj.msg} </p>
            </span>
            )
          })}
        </div>

        <form id="chatter-form">
          <input
            type="text"
            placeholder="Enter username"
            className="username-field"
            onChange={
              (event) => {
               settingname(event.target.value);
                }
            }
          />

          <button className="username-submit" >Go</button>

          <input
            type="text"
            placeholder="Enter username"
            className="message-field"
            onChange={(event) => {
              settingmessage(event.target.value);
            }}
          />

          <button className="message-submit" onClick={addChat()}>SEND</button>
        </form>
      </header>
    </div>
  );
}```
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your button is calling addChat because it has parentheses after it. Make sure you pass a reference to the function rather than call it.

You want addChat instead of addChat()

<button className="message-submit" onClick={addChat}>SEND</button>
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