Eugene
Eugene

Reputation: 10045

Restore one variable into redux from localStorage

I'd like to persist a checkbox value, which is stored in the redux. I'm trying to restore it in reducer like this, but it doesn't appear to be working, always returning false. I don't want to sync the entire app's state, only a particular value, is there a simple solution for this?

const initialState = {
  isShowingDuplicates: localStorage.getItem('isShowingDuplicates') || false,
};

Upvotes: 0

Views: 32

Answers (1)

Harish Soni
Harish Soni

Reputation: 1896

You cannnot get the value of a key from localStorage like this:

const initialState = {
  isShowingDuplicates: localStorage('isShowingDuplicates') || false,
};

The correct way to do is:

const initialState = {
  isShowingDuplicates: localStorage.getItem('isShowingDuplicates') || false,
};

Just be sure the you have already set the item into the localStorage like this:

localStorage.setItem('isShowingDuplicates', value);

Upvotes: 1

Related Questions