Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persisting State in LocalStorage #12

Closed
lynxionxs opened this issue Apr 16, 2024 · 1 comment
Closed

Persisting State in LocalStorage #12

lynxionxs opened this issue Apr 16, 2024 · 1 comment

Comments

@lynxionxs
Copy link

Is there a way to persist state in localStorage or Indexeddb or other persistent ways? Then when the app starts it loads the state again

@wobsoriano
Copy link
Owner

wobsoriano commented May 13, 2024

You can use zustand's built-in middleware for that

import { createWithSignal } from 'solid-zustand'
import { persist, createJSONStorage } from 'zustand/middleware'

export const useBearStore = createWithSignal(
  persist(
    (set, get) => ({
      bears: 0,
      addABear: () => set({ bears: get().bears + 1 }),
    }),
    {
      name: 'food-storage', // name of the item in the storage (must be unique)
      storage: createJSONStorage(() => sessionStorage), // (optional) by default, 'localStorage' is used
    },
  ),
)

Source: https://docs.pmnd.rs/zustand/integrations/persisting-store-data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants