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

221
Visualizações
React Error: Too many re-renders. while using arrow function

I am making a calculator in react in which i made buttons for numbers and when button "7" is pressed then in the input field 7 is added.

My approach:
I am using useState to do this. I made an arrow function funinpval which takes takes number as string in argument then i am using this function with different buttons onclick handler by passing respective numbers as arguments. But I am getting error error img

import React from 'react'
import { useState } from 'react';

export const Calculator = () => {
    const [inpval, setInpval] = useState("")
    
    const funinpval = (num) => {
        setInpval(inpval + num)
    }
    
   return(
   <>
     <input type="text" value={inpval}>
     <button onClick={funinpval("7")}>7</button>
     <button onClick={funinpval("8")}>8</button>
   </>
   )

Can anyone please help

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

0

     <button onClick={funinpval("7")}>7</button>
     <button onClick={funinpval("8")}>8</button>

You are not waiting the user to click the buttons to execute the functions, they are instead executed every render phase, directly. Which mean that the component render -> state update -> new re-render -> new state update -> ...

To fix it:

     <button onClick={() => funinpval("7")}>7</button>
     <button onClick={() => funinpval("8")}>8</button>
about 4 years ago · Juan Pablo Isaza Relatório

0

There is a syntax error in how you are providing the event handlers.

You have to provide event handlers sonething like:

 <button onClick={() => funinpval("7")}>7</button>
 <button onClick={() => funinpval("8")}>8</button>

Simply writing onClick={funinpval("7")} will immediately call the function while rendering which sets the state. When state got updated then the component re-renders. Then again while re-rendering, this function got called and so on.

about 4 years ago · Juan Pablo Isaza Relatório

0

onClick={funinpval("7")}

will return the result of calling that function to the listener rather than a reference to the function that the listener can call. So you're setting state immediately with those two buttons which is causing the render which is calling the function again which is setting the state again... infinity!

In this example I pick up the textContent of the button and use that to set the new input state, and then you can simply just pass the reference to the function to the handler and let the function deal with how state is set.

const { useState, useEffect } = React;

function Calulator() {

  const [inpval, setInpval] = useState(0);

  function funinpval(e) {

    // Grab the `textContent` of the button and
    // relabel it to `num` making sure to coerce the
    // text to a number first
    const { textContent: num } = e.target;
    setInpval(inpval + Number(num));
  }
    
  return(
    <div>
      <input type="text" value={inpval} />
      <button onClick={funinpval}>7</button>
      <button onClick={funinpval}>8</button>
     </div>
   )

};

// Render it
ReactDOM.render(
  <Calulator />,
  document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>

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