How can I save/load the checked state of a Menu Item in Electron?

Cassidy Williams :

I have a Menu in an Electron application, and the items look something like this:

 const menuTemplate = [
    // ... previous menu items
    {
      label: "Notifications",
      submenu: [
        {
          label: "Enable reset notification",
          type: "checkbox",
          checked: true,
          click: e => {
            mainWindow.showResetNotification = e.checked;
          }
        },
        {
          label: "Reminder notifications",
          submenu: [
            {
              label: "Never",
              type: "radio",
              click: e => {
                if (e.checked) {
                  mainWindow.resetNotification = "never";
                }
              }
            },
            {
              label: "Every 30 minutes",
              type: "radio",
              click: e => { /* ... */ }
            },
            {
              label: "Every hour",
              type: "radio",
              checked: true,
              click: e => { /* ... */ }
            }
          ]
        }
      ]
    }
  ];

I want to be able to check something, and save that state so that if I re-open the application, it loads the previous state of the checked menu item (checked vs unchecked).

How can I do this in Electron? I tried local storage, but because it's the main process, it doesn't have access to that.

Thanks in advance!

Matthew Gerstman :

Use a package like electron-store

It's less than <100 lines of code and only has a few dependencies.

https://github.com/sindresorhus/electron-store

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=12308&siteId=1