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

210
Vistas
Is there a way to avoid calling the function on hot-reload on Next.js

I have a very simple hook that works as intended:

const subscriptions: Subscription[] = []

const useEvents = () => {
  const subscribe = (type: Type, callback: () => void) => {
    const id = nanoid(8)
    subscriptions.push({
      id,
      type,
      callback,
    })
    return () => {
      subscriptions.splice(
        subscriptions.findIndex((s) => s.id === id),
        1
      )
    }
  }

  const dispatch = (type: Type) => {
    for (const subscription of subscriptions.filter((s) => s.type === type)) {
      subscription.callback()
    }
  }
  return { subscribe, dispatch }
}

I subscribe so:

useEffect(() => {
  subscribe("run", () => {
    console.log("RUN!!")
  })
}, [])

The problem is that when the app is hot-reloading subscribe is called again and the callback duplicated, is there any way to avoid this?

about 4 years ago · Juan Pablo Isaza
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