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

chrome.storage.local.get doesn't return the initial values #473

Open
Othmanosx opened this issue Jan 31, 2024 · 1 comment
Open

chrome.storage.local.get doesn't return the initial values #473

Othmanosx opened this issue Jan 31, 2024 · 1 comment

Comments

@Othmanosx
Copy link

when providing an initial value to the createChromeStorageStateHookLocal hook, and trying to access the data from chrome.storage.local.get, it returns undefined instead of the initial false.
P.S.: the hook is already mounted by the timechrome.storage.local.get was called.

export const SETTINGS_KEY = 'settings';
const INITIAL_VALUE = {
  isDark: false,
};

export const useSettingsStore = createChromeStorageStateHookLocal(SETTINGS_KEY, INITIAL_VALUE);
  chrome.storage.local.get([SETTINGS_KEY], async function (result) {
      console.log(result.isDark) // undefined
  });
@onikienko
Copy link
Owner

onikienko commented Jan 31, 2024

It's the behavior of chrome.storage.
It doesn't set an init/ial value. It only uses it if no value stored.

// say isDark not stored in storage yet (undefined)
// getting isDark and providing initial value
const options = await chrome.storage.sync.get({isDark: true});
constole.log(options.isDark); // true (initial value used)
// getting isDark without initial value
const options1 = await chrome.storage.sync.get('isDark');
constole.log(options1.isDark); // undefined (we did not set isDark yet and did not provide init value)

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