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

177
Visualizações
React web3 contract events removing listener is not possible

I am trying to create a custom hook which listens to my web3 contract events and unbinds when I change my account or network. Nonetheless it executes twice when I switch my account back and forth. Except this it works fine but for any reasons it does not remove the listener.

I have also tried removing the listener before I add it but still it will only be added and so on execute more then once.

const EVENT_OPTIONS = {
    fromBlock: "latest",
};

export const useEvent = (event, handler) => {
    useEffect(() => {
        if (!event) return;
        event(EVENT_OPTIONS).addListener("data", handler);

        return () => {
            //This gets executed but the event will not be removed
            // (Executed when the metamask network changes)
            event(EVENT_OPTIONS).removeListener("data", handler);
        }
    }, [event])
}

// ----- Example for useEvent:

export const useOnNewRound = (callback) => {
    const contract = useContractWSS(); // returns websocket contract
    
    const sortData = useCallback((data) => {
        // do something with data...
        callback(data)
    }, [])
     
    useEvent(contract?.events.NewRound, sortData); //Pass event and handler function
}
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