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

Accessing store from renderer #109

Closed
RobbieTheWagner opened this issue Apr 26, 2020 · 7 comments · Fixed by #110
Closed

Accessing store from renderer #109

RobbieTheWagner opened this issue Apr 26, 2020 · 7 comments · Fixed by #110

Comments

@RobbieTheWagner
Copy link
Contributor

I've been looking through issues about how to use the store in the renderer, and most issues say something like just use it the same as you would in main, but this is confusing to me. It appears the store is not a singleton, so if you set it up in main, there is no way to access the same store in the renderer, correct?

@sindresorhus
Copy link
Owner

#15 (And yes, I need to get that documented in the readme)

@RobbieTheWagner
Copy link
Contributor Author

@sindresorhus I would recommend documenting something like this for accessing store values from the renderer:

ipcMain.on('requestStoreValue', (event, key) => {
  event.sender.send('replyStoreValue', key, store.get(key));
});

@sindresorhus
Copy link
Owner

Agreed. Especially important as Electron plans to deprecate the remote module (remote.require).

@RobbieTheWagner
Copy link
Contributor Author

@sindresorhus would you like me to try to throw together a PR?

@sindresorhus
Copy link
Owner

@rwwagner90 That would be great. Would be best to show usage with the .invoke/.handle` APIs as they simplify this kind of communication: https://www.electronjs.org/docs/api/ipc-main#ipcmainhandlechannel-listener

@RobbieTheWagner
Copy link
Contributor Author

@sindresorhus I opened a PR. Please let me know if you would like any changes to the content!

@jeoy
Copy link

jeoy commented May 18, 2020

@sindresorhus I just found out I can set in Main process and directly get in renderer process, but why since you mentioned it's not a singleton.

// mainProcess.js
import Store from 'electron-store';
const store = new Store();

store.set('foo',  'bar');
// renderer.js

import Store from 'electron-store';
const store = new Store();

store.get('foo');
//=> 'bar'

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

Successfully merging a pull request may close this issue.

3 participants