Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Netlify profile imageCassidy Williams
Cassidy Williams forNetlify

Posted on • Originally published atnetlify.com on

     

"Escaping" Next.js to access the browser

Welcome toBlogvent, day 4!

Often when you are trying to add event listeners or DOM variables outside of a Next.js application, you’ll get a particularly unhelpful error:

ReferenceError: window is not defined
Enter fullscreen modeExit fullscreen mode

To get around this, you can use the React hook,useEffect! There’s a couple options depending on what you need.

If you just need to access the window, you can useuseEffect by itself, in something like this:

import{useEffect}from'react'functionPage(){useEffect(()=>{// use/set the window variable in here})...}
Enter fullscreen modeExit fullscreen mode

And if you need to get an object in the browser (a DOM node or something) outside of Next.js, and render something into it, you can combineuseEffect withuseRef!

importReactDOMfrom'react-dom';import{useRef,useEffect}from'react'functionPage(){letref=useRef()useEffect(()=>{ReactDOM.render(<OtherThing/>ref.current);},[])return<divref={ref}/>}
Enter fullscreen modeExit fullscreen mode

I can’t think of a use case for this!

That’s okay! It’s something that comes up for very specific cases of “escaping” Next.js (or even Gatsby or vanilla React projects), for example using event listeners, using external JavaScript libraries, or adding certain animations. If you’d like to see an example in a real codebase,check out this part of the Activity Graph in Jamstack Explorers.

Speaking of which, if you’d like to learn more about Next.js, check out the course (with more to come) onJamstack Explorers!

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
okbrown profile image
Orlando Brown
A leader, developer, father and lover of dopeness.
  • Location
    London
  • Education
    Greenwich University
  • Work
    Engineer & PM at Stepstone
  • Joined

A good use case for this is when your using 3rd party scripts. E.g. analytic scripts that require you to fire an action base on a specific event in your application. Tealium, Adobe etc.

Another one is if you are building multiple front ends that all are used within your Nextjs app or some other SSR where you will need an enclosure what contains your libs, assets etc. This can be placed on the window object where your other team/services/apps deployed in your enclosed app have access to things like React. Reducing the need for all your apps to be bundled with the same library.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

The simplest way to build modern web projects

Netlify is the fastest way to build the fastest sites.

More fromNetlify

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp