Sling Academy
Home/Next.js/Fixing `localStorage is not defined` Error in Next.js

Fixing `localStorage is not defined` Error in Next.js

Last updated: January 01, 2024

Understanding the Error

Encountering the localStorage is not defined error in Next.js happens because localStorage is a browser API, and during server-side rendering (SSR), your code is executed in a Node.js environment where localStorage is not available. To resolve such errors, it is crucial to ensure that localStorage operations are only called when the code is running on the client-side.

Ensuring Client-Side Execution

Next.js 13 introduced a more intuitive file-system routing and added features to distinguish between server and client code. Instead of using flags like use client, you can now make use of new conventions and components to control where your code should run. An effective method is to encapsulate the code that utilizes localStorage within React’s lifecycle methods that only run on the client, such as useEffect(), or using the use prefix with hooks that are designed to run on the client side.

Using React Hooks

Implement client-side only logic within React’s useEffect hook. This hook ensures the enclosed code only runs after the component mounts, which only happens on the client. Below is an example of how to safely access localStorage in a functional component that uses the useEffect hook.

import { useEffect, useState } from 'react';

export default function ComponentWithLocalStorage() {
  const [storedValue, setStoredValue] = useState(null);

  useEffect(() => {
    const data = localStorage.getItem('myKey');
    if (data) setStoredValue(JSON.parse(data));
  }, []); // Empty dependency array ensures useEffect runs once after initial render

  return (
    <div>
      {storedValue && (
        <p>Value from localStorage: {storedValue}</p>
      )}
    </div>
  );
}

Conditionally Loading Components

Another approach is to conditionally render components based on whether the code is executing on the client or the server. Next.js provides a built-in hook called useClientEffect which you can use to replace useEffect when the effect should only run on the client side. Below is an example illustrating how to use it.

import { useClientEffect, useState } from 'next';

export default function Component() {
  const [data, setData] = useState('');

  useClientEffect(() => {
    setData(localStorage.getItem('myKey') || '');
  }, []);

  return (
    <div>
      {data && (
        <p>Client-side data: {data}</p>
      )}
    </div>
  );
}

Dynamic Imports with SSR Disabled

You can also utilize dynamic imports with the { ssr: false } option to ensure that a component or module is only imported and used on the client side. This is particularly helpful when the module itself (not just its usage in your code) assumes a browser environment and could cause issues during SSR.

import dynamic from 'next/dynamic';
import React from 'react';

const ClientSideComponent = dynamic(
  () => import('./ClientOnlyComponent'), // Replace with the path to your component
  { ssr: false }
);

export default function Page() {
  return (
    <div>
      <ClientSideComponent />
    </div>
  );
}

Conclusion

By using the methods described such as leveraging useEffect, conditionally rendering with useClientEffect, or dynamically importing with SSR disabled, you can easily circumvent the localStorage is not defined error in your Next.js 13+ projects. It’s essential to think about where and how your code runs, separating client-side operations from server-side whenever necessary.

Next Article: Fixing Next.js Error: Cannot Load Images from Public Folder

Previous Article: Fixing Refs in Functional Components with Next.js

Series: Common Errors in Next.js

Next.js

Related Articles

You May Also Like

  • Solving Next.js Image Component Error with CSS Classes
  • How to Use MUI (Material UI) in Next.js 14
  • Fixing Data Update Issues in Next.js Production
  • Fixing Next.js Undefined ENV Variables in Production Build
  • Solving Next.js SyntaxError: Unexpected token ‘export’
  • Next.js Error: Found Page Without a React Component as Default Export – How to Fix
  • Fixing Next.js Error: Blank Page on Production Build
  • Fixing Next.js Error when Importing SVGs: A Step-by-Step Guide
  • Next.js Issue: useEffect Hook Running Twice in Client
  • Fixing ‘self’ is not defined error in Next.js when importing client-side libraries
  • How to Dockerize a Next.js App for Production
  • How to set up Next.js with Docker Composer and Nginx
  • Solving Next.js CORS Issues: A Comprehensive Guide
  • Fixing Next.js Hydration Mismatch Error in Simple Ways
  • Next.js Error: Cannot use import statement outside a module
  • Next.js: How to Access Files in the Public Folder
  • Fixing Next.js Import CSS Error: A Simple Guide
  • Fixing LocalStorage Not Defined Error in Next.js
  • Fixing Next.js Error: No HTTP Methods Exported