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

140
Vistas
invoking only existing method

I have a ref that contains a method that turns window into full screen. However, this method name changes depending the browser:

  const setFullscreen = () => {
    if (!elRef.current) return;

    elRef.current
      .requestFullscreen() // webkitRequestFullScreen on Safari
      .then(() => {

        setIsFullscreen(document[getBrowserFullScreenElementProp()] != null);
      })
      .catch(() => {
        setIsFullscreen(false);
      });
  };

As you can see, Safari uses webkitRequestFullscreen, so the above code doesn't work in Safari.

How can I make the code work keeping it DRY? Found something similar here (link) but it won't help keeping it DRY.

I was thinking in something like:

const requestFullScreen = elRef.current.requestFullscreen || elRef.current.webkitRequestFullscreen;

requestFullScreen().then .... more

However this doesn't invoke the method in neither of the browsers.

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

0

Probably You need to bind method to the target before calling it:

const requestFullScreen = (elRef.current.requestFullscreen || elRef.current.webkitRequestFullscreen).bind(elRef.current);

requestFullScreen().then .... more

You can also add the failsafe with || () => {} at the end to not crash when both requestFullscreen and webkitRequestFullscreen do not exist:

const requestFullScreen = (elRef.current.requestFullscreen || elRef.current.webkitRequestFullscreen || () => {}).bind(elRef.current);

requestFullScreen().then .... more
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