Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!