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

182
Vistas
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 Respuestas
Responde la pregunta

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 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