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

183
Visualizações
window.print() not working inside useeffect (or anywhere on page)

I am trying to call window.print() and nothing happens. In the same place, I try window.alert('test') and it works.

any ideas?

a different page in my application does successfully call window.print() so I'm thinking it's not a printing setting on my machine

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

window.print was being blocked by an element on the page.

I got it to work using the react-to-print. If you look at the code behind react to print, you'll see it doesn't quite use window.print, rather some other thing like (target.contentWindow.print();). And in the readme of it, you can see it was explicitly created for scenarios where window.print was blocked - like firefox. enter image description here

Implementation of this is like this:

  1. add a useRef variable and set it to an element on the page (either a div perhaps or else a component.

const componentRef = useRef(); const handlePrint = useReactToPrint({ content: () => componentRef.current!})

<div css={{ display: 'none', '@media print': { display: 'block' } }} ref={componentRef}> <CoversheetPrint {...coversheetData} />         
</div>

https://github.com/gregnb/react-to-print/search?q=window.print

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