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

150
Views
Why does adding extra arrow function parentheses resolve an infinite loop problem?

I'm working on my project using react but I encountered a problem with using setState hook.

Here is my code:

//state and handle function

const [activeStep, setActiveStep] = React.useState(0);
const handleStep = (index)  => {
        setActiveStep(index);
}

//return component (I'm using mui)

return (
            <Stepper nonLinear activeStep={activeStep}>
                {steps.map((label, index) => (
                    <Step key={label} completed={false}>
                        <StepButton color="inherit" onClick={handleStep(index)}>
                            {label}
                        </StepButton>
                    </Step>
                ))}
            </Stepper>

    );

With this code page enters infinite loop and the page doesn't show up. But if I add an extra parentheses to handlestep function:

//added extra parantheses
const handleStep = (index) => () => {
        setActiveStep(index);
    }

It doesn't enter infinite loop and everything seems working. Does adding parentheses to arrow functions changes functionality or is it a MUI component bug?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are calling function instead of passing.

Change this

<StepButton color="inherit" onClick={handleStep(index)}>

Into this

<StepButton color="inherit" onClick={() => handleStep(index)}>
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!