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

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

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 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