Estoy tratando de crear un gancho personalizado que escuche los eventos de mi contrato web3 y se desvincule cuando cambio mi cuenta o red. No obstante, se ejecuta dos veces cuando cambio mi cuenta de un lado a otro. Excepto esto, funciona bien, pero por alguna razón no elimina al oyente.
También intenté eliminar el oyente antes de agregarlo, pero aún así solo se agregará y así sucesivamente se ejecutará más de una vez.
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 }