Giter Site home page Giter Site logo

window is not defined about react-to-pdf HOT 7 CLOSED

ivmarcos avatar ivmarcos commented on July 16, 2024
window is not defined

from react-to-pdf.

Comments (7)

ivmarcos avatar ivmarcos commented on July 16, 2024 2

@yantakus, since you are using Next.Js, try using some strategy regarding NoSSR components. For example: https://nextjs.org/docs#with-no-ssr.

from react-to-pdf.

ltakato avatar ltakato commented on July 16, 2024 1

@yantakus, since you are using Next.Js, try using some strategy regarding NoSSR components. For example: https://nextjs.org/docs#with-no-ssr.

@ivmarcos next team update their page, on nowadays this kind of info is located here:
https://nextjs.org/docs/advanced-features/dynamic-import#with-no-ssr

from react-to-pdf.

ivmarcos avatar ivmarcos commented on July 16, 2024

Are you using React on server or front end?

from react-to-pdf.

TonyBrobston avatar TonyBrobston commented on July 16, 2024

I'm importing it into a React project. Let me know if there are any other questions I can answer.

from react-to-pdf.

ivmarcos avatar ivmarcos commented on July 16, 2024

Hi @TonyBrobston! This type of error related to the message window is not defined happens when you are using on the server side (node) and not in the browser. That's why I asked if you were using React on the server, because this lib is intended to be used only in the browser, SSR is not supported, sorry.

from react-to-pdf.

yantakus avatar yantakus commented on July 16, 2024

I don't need it to render server side, but in my case I'm using next.js and the app is rendered initially at server. I thought I can do smth like this:

{process.browser && (
<ReactToPdf>
    {({toPdf, targetRef}) =>  (
        <div style={{width: 500, height: 500, background: 'red'}} onClick={toPdf} ref={targetRef}/>
    )}
</ReactToPdf>
)}

but it's not possible, because I'm getting window is not defined even when I'm just importing the library and not using it.

Why not doing something like this

if (window) {
...
}

before using it?

from react-to-pdf.

gucal avatar gucal commented on July 16, 2024

As @ltakato said, you will not have this problem when you use dynamic import.

Example:

import dynamic from 'next/dynamic'

const ReactToPdf = dynamic(() => import('react-to-pdf'), { ssr: false })

from react-to-pdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.