Matthew C.
—In Next.js version 13+, you may have encountered the following error when trying to use the browser API window
object:
Unhandled Runtime Error Error: window is not defined
This error occurs because components are server-rendered by default. Components are pre-rendered into HTML on the server before being sent to the client. The window
object is not available on the server.
If you want to use browser APIs like window
, use a client component. To make your component a client component add the React "use client"
directive at the top of your component file, above your imports.
The client components will still be pre-rendered on the server and then hydrated on the client. To make sure that the code that uses the window
object only runs on the client, use the useEffect
hook, an event handler, lazy load the component, or do a check for the window
object.
useEffect
You can access the window
object once the component is mounted inside a useEffect
callback function:
useEffect(() => { window.alert("window.alert from client component"); }, []);
You can also access the window
object in event handlers because event handlers are added to the pre-rendered HTML on the client by React. This process is called hydration.
<button onClick={() => window.alert("window.alert from client component")}> Click me </button>
window
is DefinedYou can also check if the window
object is defined:
if (typeof window !== "undefined") { window.alert("window.alert from client component"); }
Be careful about using this method as it can cause hydration errors if it causes a difference between the HTML that’s pre-rendered on the server and the HTML that’s first rendered on the client when the component is hydrated.
If you want to access the window
object at the top level of your component, you can lazy load your client component and disable pre-rendering on the server to make sure that it’s only rendered on the client. However, this may make the initial page load slower. This approach is useful when using client-only libraries or if you only need to load the client component after a certain user action.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.