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

149
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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