What is the best way to utilize localStorage within store/state, particularly in state with Nuxt.js?

I am facing an issue with accessing localStorage within the store, particularly in state.
I have noticed that it is not possible to access browser methods/objects during SSR.

Here is my attempt:

export const state = () => {
  if (process.client) {
    return {
      isAuth: localStorage.getItem('isAuth')
    }
  } else {
    return {
      isAuth: 'SomeData'
    }
  }
}

The Vuex development console shows as empty, no data is displayed. However, when I try the following:

export const state = () => {
  if (process.client) {
    return {
      isAuth: localStorage.getItem('isAuth')
    }
  }
}

The server works correctly and sets isAuth = 'SomeData', but I specifically need the "client" side functionality. How can I successfully access localStorage?

Answer №1

One possible solution is to retrieve the local storage data within the mounted hook and then use it to update your Vuex state.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a clash with AJAX while attempting to incorporate PayPal

I am currently facing an issue while trying to integrate PayPal payment into my website. The problem arises when I attempt to use Ajax to send shopping cart data to create an order, resulting in the following alert message: Failed to load Response to pref ...

If the div element undergoes a change, use an if-else condition to populate the data fields accordingly

I'm looking to create a basic if/else statement in JavaScript that checks if a div element has changed before populating JSON data fields/variables that pull from dynamically updated HTML. I want to avoid using the deprecated DOMSubtreeModified metho ...

React - Managing multiple requests using Ajax

Within my react application, there is a Grid feature that allows users to select multiple rows at once and then perform a bulk action on them by clicking a button. Running on the server side, I have a script that needs to be executed for each selected row ...

The technique for converting a JSON format date string into a date format

Currently, I am fetching data from a database using AJAX and displaying it in HTML text boxes for updating purposes. Below is the Web Method code that successfully retrieves the data: [WebMethod] public static List<Employee> getEmployee() { var ...

I'm curious about where to find more information on this new technology called the 'scroll to page' feature

Recently, I came across a captivating website feature that caught my eye. As someone relatively new to front end web development, I am intrigued by the scrolling technique used on this site. I'm specifically drawn to the 'page to page' scro ...

output the array utilizing map function results in undefined

I have an array structured like this: const HeaderData = [ { header: { title: "How you`ll benefit", detail: "We create these goals to assist you in recovering from your recent surgery. Slowly increasing your physica ...

Next.js encountering page not found error due to broken link URL

Currently, I am working on implementing a login system in next.js. In the login page, I have included a Link to the Register page using the Link href attribute. However, every time I click on that link, it displays a message saying "404 page not found." Al ...

Submitting information to a server

I have a simple Angular 6 app with follow and unfollow buttons. When you click follow, the number increases. I want to save these follower numbers to a JSON server. Here is the link to the JSON server documentation: JSON Server Documentation To see a dem ...

How can you implement a switchable theme feature similar to Twitter on your website?

As a beginner programmer and college student with limited design knowledge, I am working on creating a microblogging website similar to Twitter. I noticed that some websites use pre-loaded styles or themes without separate CSS files. I am unsure of how t ...

The File Filter feature of Angular's ng2-file-upload is not functioning correctly for PNG files in the Internet Explorer

I am encountering an issue with the file uploader plugin ng2-file-upload when trying to upload a PNG file using Internet Explorer 11. For some reason, the PNG files are not being added to the queue, even though all other allowed file types work perfectly f ...

Is it possible to remove certain 'css-' class names that MUI automatically applies to its components? (Using Next JS and MUI)

After successfully migrating my create-react-app project to Next JS using the latest version (12.1.0) and following the migration guide at https://nextjs.org/docs/migrating/from-create-react-app, I encountered an unexpected issue. Despite still using MUI a ...

After successfully posting a tweet, I have not heard back from Twitter

My objective is to achieve the following on click: Share the URL and text on Twitter in a popup window Receive a response from Twitter indicating whether the tweet was successful or failed Close the popup window after the tweet is successfully pos ...

Vee Validate offers both server-side and client-side validation services for ensuring data accuracy and security

I'm encountering an issue with my Laravel Lumen endpoint where VeeValidate is not setting the errors properly. The errors are being returned correctly from the endpoint, but they are not being displayed in the view. <template> <v-container ...

Injecting JavaScript page level variable into an integrated Angular2 RC 1 application

To keep this brief, the issue I'm facing is an extension of a Stack Overflow question regarding passing page-level variables into an Angular 2 app service. Following Gunter's advice from the provided SO question, I attempted to pass a JavaScript ...

Issue with Sequelize failing to update a row within a database table

This is my first experience using sequelize. Within my database, I have a table named feed containing columns such as ID, caption, and url. In my controller, there is a straightforward function aimed at updating a row within this table: router.patch(' ...

Error message encountered while using Node.JS IPFS: TypeError occurs when attempting to send a message, as it cannot read undefined properties related to 'publish

Every time I attempt to send a message over the IPFS network, I encounter the following error message: TypeError: Cannot read properties of undefined (reading 'publish'). This error crops up at line number node.pubsub.publish(topic, msg, (err) = ...

I can't seem to figure out why my characters keep disappearing from the HTML string when I attempt to dynamically add HTML using JavaScript

I am currently facing an issue with dynamically adding links to a page. The links are being added successfully, however, the '[' and ']' at the beginning and end of the line are disappearing. Here is the code snippet from my .js file: ...

Tips for making a dropdown menu within an Ionic Side menu

Looking for guidance on creating a sub menu in the ionic framework? I'm new to AngularJS and ionic framework, but eager to learn. Below is the code snippet I used to create a dropdown list component: <ion-side-menu side="left"> <ion-co ...

I have no interest in using vanilla JavaScript to search for data in the database

I have a class assignment that requires the use of XMLHTTPREQUEST and JSON without using Jquery. The task involves searching for specific data through custom inputs. However, when I perform the search, I encounter this error which I'm unsure how to r ...

I have successfully configured the div to show only one at a time, however, I am currently facing an issue with turning off the div

My goal is to disable all annotations within a specific div class and then enable just one particular ID afterwards. This method is meant to ensure that only one annotation can be open at any given time. However, I have encountered an issue with the Javasc ...