---
title: "How to “Escape” Next.js to Access the Browser"
description: "Sometimes you need what’s called an “escape hatch” to access certain browser properties and methods in a Next.js application. Here’s how to make one!"
source: "https://www.netlify.com/blog/2020/12/04/escaping-next.js-to-access-the-browser/"
last_updated: "2026-08-29T17:11:34.000Z"
---
Welcome to [Blogvent](https://www.netlify.com/blog/tags/blogvent/), 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
```

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 use `useEffect` by itself, in something like this:

```
import { useEffect } from 'react'
function Page() {  useEffect(() => {    // use/set the window variable in here  })  ...}
```

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 combine `useEffect` with `useRef`!

```
import ReactDOM from 'react-dom';import { useRef, useEffect } from 'react'
function Page() {  let ref = useRef()
  useEffect(() => {    ReactDOM.render(<OtherThing/> ref.current);  }, [])
  return <div ref={ref}/>}
```

## 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](https://github.com/netlify/explorers/blob/main/src/components/UserActivityGraph.js#L25-L34) in Jamstack Explorers.

Speaking of which, if you’d like to learn more about Next.js, check out the course (with more to come) on [Jamstack Explorers](https://www.youtube.com/watch?v=0qXjtznmhDI&list=PLzlG0L9jlhENGgDUr09a7JdRgSTybmE1P)!

### Share

-   [X (fka Twitter)](https://twitter.com/intent/tweet?text=“Escaping” Next.js to access the browser&url=https://www.netlify.com/blog/2020/12/04/escaping-next.js-to-access-the-browser//)
-   [LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fwww.netlify.com%2Fblog%2F2020%2F12%2F04%2Fescaping-next.js-to-access-the-browser%2F%2F)
-   [Facebook](https://www.facebook.com/sharer.php?u=https://www.netlify.com/blog/2020/12/04/escaping-next.js-to-access-the-browser//)
-   [Bluesky](https://bsky.app/intent/compose?text=“Escaping” Next.js to access the browser+https://www.netlify.com/blog/2020/12/04/escaping-next.js-to-access-the-browser//)

* * *

### Tags

-   [Next.js](/blog/tags/next.js/)
-   [blogvent](/blog/tags/blogvent/)

## Keep reading

![](/images/blog-fallback-thumbnail.svg)

Guides & Tutorials December 2, 2020

[

### Next.js: Should I use SSR or SSG?

](/blog/2020/12/02/next.js-should-i-use-ssr-or-ssg/)

-   ![Profile picture of Cassidy Williams](/_astro/a62099fab0f946e063c4b84ff4b4d9c94f9aa7a5-400x400_ZdakPa.webp)
    
    Cassidy Williams
    

![](/_astro/3f45eb6eda4ea8814be310e3df4a7883a5bd9ba0-1200x675_ZcBDUS.webp)

Guides & Tutorials May 15, 2026

[

### How to build a real-time AI chatbot in minutes with Netlify Agent Runners (no backend)

](/blog/how-to-build-a-real-time-ai-chatbot-in-minutes-with-netlify-agent-runners-no-backend)

-   ![Profile picture of Nahrin Jalal](/_astro/f0e7c8f227a03fe58340c99ef5439d5a896c0733-272x272_Z23kDpD.webp)
    
    Nahrin Jalal
    

## Recent posts

News & Announcements August 25, 2026

[

### Compete in OpenAI’s WebMCP Challenge with Netlify

](/blog/compete-openai-webmcp-challenge)

-   ![Profile picture of Karthik Puvvada](/_astro/e5524d3c315ad5114e1d5300d8991bfe902916fe-192x192_Z1w18MB.webp)
    
    Karthik Puvvada
    

News & Announcements August 19, 2026

[

### New clarifying questions in Agent Runners

](/blog/new-clarifying-questions-in-agent-runners)

-   ![Profile picture of Taylor Barnett-Torabi](/_astro/cf4624da8c1286738397b6fecb53bad504df140b-400x400_ZsvK0s.webp)
    
    Taylor Barnett-Torabi
    

Opinions & Insights August 14, 2026

[

### The full power of Git, without the friction: A conversation with Netlify CTO Dana Lawson

](/blog/netlify-source-with-netlify-cto-dana-lawson)

-   ![Profile picture of Dana Lawson](/_astro/856bf146d0c05c9dc25d45b59f7eac955fbbd644-512x512_1n84rs.webp)
    
    Dana Lawson
    

![](/_astro/3f255b372fa958df35802666ee33b4609b2d71bd-1200x1586_1VtE2D.webp)

### How do the best dev and marketing teams work together?

[Access the report](https://www.netlify.com/reports/2024-leadership-trend-report/access/)