Skip to content

Instantly share code, notes, and snippets.

@yaakaito
Last active January 18, 2024 10:00
Show Gist options
  • Save yaakaito/40acce83c42db9ec5a3f2f0d0ac97992 to your computer and use it in GitHub Desktop.
Save yaakaito/40acce83c42db9ec5a3f2f0d0ac97992 to your computer and use it in GitHub Desktop.
zustand を chrome 拡張の storage で永続化する

StateStorage を実装することで自由なストレージを設定できる。

import { StateStorage } from 'zustand/middleware';

export const zustandChromeStorage: StateStorage = {
    getItem(name) {
        return new Promise((resolve) => {
            chrome.storage.local.get([name], (result) => {
                resolve(result[name]);
            });
        });
    },
    setItem(name, value) {
        return new Promise((resolve) => {
            chrome.storage.local.set({ [name]: value }, () => {
                resolve();
            });
        });
    },
    removeItem(name) {
        return new Promise((resolve) => {
            chrome.storage.local.remove([name], () => {
                resolve();
            });
        });
    },
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment