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

253
Vistas
Is there a way to import a function using Next.js dynamic import? react-component-export-image issues with Next.js ssr

I was getting the 'window is not defined' error when importing react-component-export-image so I used a dynamic import to get around that. I don't get that error anymore but now I get 'exportComponentAsPNG(componentRef) is not a function'. Is there a better way to deal with the 'window is not defined' error or a way to use the function I am importing dynamically? If not, is there a different npm library that works to generate an image from a react component?

import React, { useRef } from 'react'
// import { exportComponentAsPNG } from 'react-component-export-image' *This gave window not defined error so I used dynamic import*
import dynamic from 'next/dynamic'
import ProductCard from '../ProductCard/ProductCard.component'
import Button from '../Button/Button.component'

const { exportComponentAsPNG } = dynamic(
  () => import('react-component-export-image'),
  {
    ssr: false
  }
)

const Plaque = () => {
 const componentRef = useRef()

  // eslint-disable-next-line react/display-name
  const ComponentToPrint = React.forwardRef((props, ref) => {
    return (
      <div ref={ref}>
        <ProductCard />
      </div>
    )
  })

  return (
        <ComponentToPrint ref={componentRef} />
          <button onClick={() => exportComponentAsPNG(componentRef)}> // "Error: exportComponentAsPNG is not a function"
          Export As PNG
        </button>
  )
}

export default Plaque
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

next/dynamic is used to dynamically import React components, not regular JavaScript functions or libraries.

For that, you can use a regular dynamic import on exportComponentAsPNG inside the onClick callback.

<button onClick={async () => {
    const { exportComponentAsPNG } = await import('react-component-export-image')
    exportComponentAsPNG(componentRef)
}}>
about 4 years ago · Juan Pablo Isaza Denunciar

0

The exportComponentAsPNG function needs access to window which is undefined with server side rendering. I was able to fix the issue by dynamically importing the Plaque component that used exportComponentAsPNG to the page where it is called with sever side rendering set to 'false'.

import dynamic from 'next/dynamic'

const Plaque = dynamic(() => import('../compnonents/Plaque'), {
  ssr: false
})

const Page = () => {
  return <Plaque />
}

export default Page

Now that the component is no longer using SSR I was able to import and use the function normally.

import { exportComponentAsPNG } from 'react-component-export-image'

Here you can find the documentation for the library: https://www.npmjs.com/package/react-component-export-image

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