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

158
Views
¿Por qué agregar paréntesis de función de flecha adicional resuelve un problema de ciclo infinito?

Estoy trabajando en mi proyecto usando reaccionar, pero encontré un problema al usar el gancho setState.

Aquí está mi código:

 //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> );

Con este código, la página ingresa al bucle infinito y la página no aparece. Pero si agrego un paréntesis adicional a la función handlestep:

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

No entra en bucle infinito y todo parece funcionar. ¿Agregar paréntesis a las funciones de flecha cambia la funcionalidad o es un error del componente MUI?

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

0

Está llamando a la función en lugar de pasar.

Cambia esto

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

Dentro de esto

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