Skip to content

Cannot read properties of null (reading 'setItem') #1799

Answered by dai-shi
bhoodream asked this question in General
Discussion options

You must be logged in to vote

@dbritto-dev I think that is usage issue, so improving docs should help.

This pattern should be something we can recommend in docs.

const myStorage = {
  getItem(name) {
    const value = JSON.parse(localStorage.getItem(name));
    // preferably validate value
    return value;
  },
  setItem(name, value) {
    try {
      localStorage.setItem(name, JSON.stringify(value));
    } catch (e) {
      console.warn(...);
    }
  },
  removeItem(name) {
    localStorage.removeItem(name);
  },
};

export const useBearStore = create(
  persist(
    (set, get) => ({
      bears: 0,
      inc: () => set((state) => ({ bears: state.bears + 1 })),
    }),
    {
      name: 'bear-storage',
      storage: 

Replies: 2 comments 19 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
19 replies
@dbritto-dev
Comment options

@bhoodream
Comment options

@dbritto-dev
Comment options

@dai-shi
Comment options

Answer selected by bhoodream
@bhoodream
Comment options

@dbritto-dev
Comment options

@dai-shi
Comment options

@dbritto-dev
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #1798 on May 11, 2023 10:06.