How to use localStorage in a NextJs or React application

The aim of this article is to teach you how to use localStorage in a Next.JS or React application. Indeed, there are a few differences with the normal use, in a vanilla application.
•
Updated on August 14, 2026
•
3 min read
Believemy logo

We will discover here how to add values to local storage, how to retrieve them, or delete them.

 

Table of Contents

 

Storing Data

To add data to local storage, use the setItem() method of localStorage. The syntax is as follows:

JAVASCRIPT
localStorage.setItem(key, value)
  • key is the key, representing the name associated with the stored value. This is how you will access it later.
  • value is the value to store, such as a theme or something else. Here, you must provide a string.

For example, for a dark or light theme:

JAVASCRIPT
localStorage.setItem('ThemeDark', 'true');

 

Retrieving Data

To retrieve a stored value, use the getItem() method. The syntax is as follows:

JAVASCRIPT
localStorage.getItem(key)

With this code, you target a specific key whose value you want to retrieve. The value will be returned as a string. If you want to retrieve a value of another type, like a boolean, in the example, you must use JSON.parse() to convert the string into a boolean.

JAVASCRIPT
JSON.parse(localStorage.getItem('ThemeDark'))

 

Deleting a Key-Value Pair

You can also delete a stored value by using the removeItem() method. It takes the key to delete as a parameter.

JAVASCRIPT
 localStorage.removeItem(key)

In this way, the key and its corresponding value are removed from local storage. Using our example:

JAVASCRIPT
 localStorage.removeItem('ThemeDark')

 

Clearing Local Storage

To clear local storage, use the clear() method. It removes all stored key-value pairs.

JAVASCRIPT
localStorage.clear()

 

Retrieving the Key of a Pair

You can use the key() method to find the key of a value at a given index in local storage.

JAVASCRIPT
localStorage.key(index)

For example, if you want to retrieve the name of the second key:

JAVASCRIPT
localStorage.key(1)

 

Knowing How Many Items are Stored

Finally, you can know the size of local storage using the length() method. This way, you get the number of key-value pairs present in local storage.

JAVASCRIPT
localStorage.length()

 

 

And the bugs?

You might encounter this error message: ReferenceError: localStorage is not defined. To fix it, simply check if window is defined using a condition.

JAVASCRIPT
if (typeof window !== "undefined") {
  value = localStorage.getItem(key)
}

Additionally, if you want to retrieve a value, I highly recommend using a useEffect to ensure that the method executes at the right time.

 

Therefore, it's very simple to use local storage with React or Next. You can implement it in a React Context, for example, to set up a theme system, as we do here.

If you want to learn everything about React, take a look at our comprehensive React course.

Category: Developers

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.

Believemy logo
Comments (0)

You need to be logged in to comment on this article: log in or sign up.

Read next

Respecting SOLID principles with React

The SOLID principles are a set of five fundamental programming concepts. They are mostly applied in OOP (Object-Oriented Programming). They can also be applied elsewhere. They are indispensable in software architecture. To present these principles, I've chosen to use Javascript rather than Typescript, so as not to weigh down the examples. We're going to apply these principles to a simple "Todo List".

December 11, 2024

How to use Server Actions with NextJS

Discover why and how NextJS Server Actions improve performance on both Client Components and Server Components, and learn how to use them in your projects.

December 9, 2024

Understanding .env files with ReactJS

Find out how to really use the .env file with React or NextJS so you never make a mistake again.

December 9, 2024

Implementing over-request revalidation with NextJS

If you're a web developer, you probably know how important it is to have a fast, responsive site to deliver an optimal user experience. In this article, we're going to show you how to set up on-demand revalidation with NextJS, a feature that refreshes your site's data only when necessary. Using this method, you can save resources and improve the performance of your website, while offering up-to-date data to your users.

December 9, 2024

Setting up SCSS themes and contexts with NextJs or React

You can't modify a scss variable, because scss converts its code to css, applying the variable values directly. This makes it difficult to set up a light/dark theme on your React or Next project and scss. Fortunately, there are solutions, which we'll be exploring in this article.

December 11, 2024

Setting up a loader when changing a Nextjs page

Have you ever been on a site where you click on a link and nothing happens? Or perhaps you've wondered whether your request was actually received? This is where having a loader comes in. In this article, we'll explain why it's important to have a loader on your website, how to set it up in your Nextjs project and create a function that will display the loader until the page is loaded. If you want to improve the user experience on your website and reassure your visitors, this article is for you.

December 11, 2024

React vs NextJS: what should you choose for your project in 2025?

Which technology should you use, React or NextJS? Discover their advantages and disadvantages to choose the one best suited to your projects.

December 9, 2024

Send an email with NextJS and SendGrid for free

Have you always wanted to send emails with JavaScript? Or more specifically, how to send emails with React or NextJS? Then let's find out together how to use SendGrid! It's a quick and easy solution to set up that will help us ensure that our emails are sent and, most importantly, received.

December 9, 2024

Become the solopreneur you dream of being.

First project, career change, business launch: access our courses, practice on real-world cases, and move forward with clear guidance.