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

142
Vistas
How to avoid React Hooks inside loops and conditions?

I have the following problem: I am using an Use Effect Hook to update my taskList when I open my WebApp. So I have to lists a list with task which has the attribute recurring tasks (recuringTaskManager.lastWeeklyReset) and a list which can have attributes with recurring and without (taskList). So my goal is to update taskList with the other one when a special condition occur (if differenceInDays....). so I want to delete some tasks in Taskslist therefore the filter function and add them again so that a special attribute can get refreshed. I found out that using hooks in conditions and loops is always bad but I don't know how I could code it without loops and conditions.

Maybe you know

useEffect(() => {
    if (!isInitialized) return
    if (differenceInDays(new Date("July 28, 2022 00:00:00"), state.recuringTaskManager.lastWeeklyReset) >= 7) {
      for (let task of state.recuringTaskManager.recuringTaskListWeekly) {
        const {id} = task
        setState({...state, taskList: state.taskList.filter((task: Task) => task.id !== id)})
state.recuringTaskManager.recuringTaskListWeekly)
      }

      setState({...state, taskList: [...state.recuringTaskManager.recuringTaskListWeekly, ...state.taskList]})
    }
  }, [isInitialized])
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

What you can do to avoid that is to set a variable and increment it in the loop and then set the state outside the loop or I think for your case you can just do this in one line

setState({...state, taskList: state.taskList.filter(({id}) => !state.recuringTaskManager.recuringTaskListWeekly.some(({id: id2}) => id === id2))})

The some method return true if one of the item of the array met the condition, if not it returns false

about 4 years ago · Santiago Trujillo 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