Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda