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

119
Vistas
Javascript removeEventListener on dynamic components

so my code looks like

useEffect(() => {
    const element = document.getElementById('player');
    document.getElementById('fullscreen').addEventListener('click', () => {
        if (screenfull.isEnabled) {
            screenfull.request(element);
        }
    });
    document.getElementById('fullscreen-out').addEventListener('click', () => {
        if (screenfull.isEnabled) {
            screenfull.toggle(element);
        }
    });
    return () => {
        document.getElementById('fullscreen').removeEventListener('click', () => {
            if (screenfull.isEnabled) {
                screenfull.request(element);
            }
        });
        document.getElementById('fullscreen-out').removeEventListener('click', () => {
            if (screenfull.isEnabled) {
                screenfull.toggle(element);
            }
        });
    }
}, [])

The content platform I'm building has a master state that the admins can change at any time, some components, (such as in the example code) will not be displayed&rendered on the certain state.

Now the problem then lies in the detachment of a event listener. When the state changes, the component is then ripped out of the DOM and the event listener then cannot be removed(in my understanding).

So this causes the following error TypeError: Cannot read properties of null (reading 'removeEventListener')

How can I detach the listener when the component exits?

Any help is appreciated, thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As Dai pointed out in his comment, I should've let React do the work and use the event handlers the framework provides.

onClick is an event handler by itself, and does the exact same thing as the code I provided in my first example.

Thanks Dai.

function requestScreenfull() {
    const element = document.getElementById('player');
    if (screenfull.isEnabled) {
        screenfull.request(element);
    }
}

function toggleScreenfull() {
    const element = document.getElementById('player');
    if (screenfull.isEnabled) {
        screenfull.toggle(element);
    }
}


 <button onClick={requestScreenfull}>Fullscreen</button>
 <button onClick={toggleScreenfull}>Toggle</button>

More information on React Documentation

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