Why isn't client available on vuex state. Nuxt

Why can’t I do something like this on nuxt js

const state = () => {
user: process.client ? localStorage.getItem(“user”) : null
}

It always returns null meaning process.client is false but it works on action and getters but not on the state.

It also works on regular vuejs I just can’t get it work.
I tried commiting a mutation to set the data when the app loads but user is undefined when I refresh the browser on pages where I output the user data. It doesn’t wait for the mutation.

It always returns null meaning process.client is false

Is this true? Is this evaluation process.client ? localStorage.getItem(“user”) actually happening?

I would set the default state to null then have a separate Action + Mutation to run this evaluationprocess.client ? localStorage.getItem(“user”) at an appropriate point from within the vue instance. Maybe after everything is mounted or created.

Yes I did that and it worked.
However, on the dashboard page for users, I have a getter that returns the current authenticated user from the state which was previously mutated but when I refresh the browser while being on that page, user becomes null and all it’s properties like user.name is undefined. It does not wait to run the evaluation and call the mutation to set the user.
It works well when I navigate to the page tho

So if you navigate to a page everything works :white_check_mark:
Refreshing said page returns the state to null/undefined :x:

Without seeing any code I would guess that you have tied the state changes to navigation or some other logic. Decoupling that so that mounting/creating the component triggers the same state update would be my next step.

However if you want to consistently pull the previous state from localStorage I would separate that logic out. Seems like the intended goal but there’s a bug somewhere. https://stackoverflow.com/questions/43047219/vuex-store-empty-if-i-refresh-my-webpage <- this sounds like the behavior you described. On refresh the store reverts to its initial state.

to summarize, I’d do one of the following:

  1. duplicate the logic that works when navigating to the page and put it somewhere in a lifecycle hook for the component you want :man_shrugging: best guess
  2. decouple and unit test the localStorage logic that picks up the prior state. (I’m inferring that’s your goal here)
  3. post more code examples