Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

211
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda