{"id":20966860,"url":"https://github.com/morintd/electron-state-ipc","last_synced_at":"2025-05-14T09:33:14.766Z","repository":{"id":37047293,"uuid":"422710070","full_name":"morintd/electron-state-ipc","owner":"morintd","description":"Easily synchronize state between main process and renderer in Electron, using your favorite UI framework:","archived":false,"fork":false,"pushed_at":"2022-07-11T18:38:07.000Z","size":2722,"stargazers_count":8,"open_issues_count":11,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-21T23:03:37.180Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/morintd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-29T20:52:28.000Z","updated_at":"2024-11-29T10:43:27.000Z","dependencies_parsed_at":"2022-08-29T01:51:07.389Z","dependency_job_id":null,"html_url":"https://github.com/morintd/electron-state-ipc","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morintd%2Felectron-state-ipc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morintd%2Felectron-state-ipc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morintd%2Felectron-state-ipc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morintd%2Felectron-state-ipc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/morintd","download_url":"https://codeload.github.com/morintd/electron-state-ipc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254112486,"owners_count":22016775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T03:01:23.585Z","updated_at":"2025-05-14T09:33:09.756Z","avatar_url":"https://github.com/morintd.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# electron-state-ipc\n\n[![npm](https://img.shields.io/npm/v/electron-state-ipc)](https://www.npmjs.com/package/electron-state-ipc)\n[![Build](https://circleci.com/gh/morintd/electron-state-ipc.svg?style=shield)](https://app.circleci.com/pipelines/github/morintd/electron-state-ipc)\n[![npm](https://img.shields.io/npm/dm/electron-state-ipc)](https://www.npmjs.com/package/electron-state-ipc)\n\nEasily synchronize state between main process and renderer in Electron, using your favorite UI framework:\n\n- React\n- Vue (coming soon)\n- Angular (coming soon)\n\n![Example in practice](https://github.com/morintd/electron-state-ipc/blob/master/example.gif?raw=true)\n\n## Installation\n\n**yarn**\n\n```sh\n$ yarn add electron-state-ipc\n```\n\n**npm**\n\n```sh\n$ npm add electron-state-ipc --save\n```\n\n## Usage\n\nAfter [configuration](#configuration), you will be able to use `electron-state-ipc` with:\n\n### React\n\nYou can access the following hooks. They all imitate default React hook, but includes an additional key. It's used to track the specific state you're trying to access (`'foo'` in the following examples):\n\n#### useStateIPC\n\nThis is an equivalent to `useState`.\n\n```tsx\nimport { useStateIPC } from 'electron-state-ipc/react';\n\nfunction Example() {\n  const [foo, setFoo] = useStateIPC\u003cstring\u003e('foo', '');\n\n  return (\n    \u003cdiv\u003e\n      \u003cp\u003eFoo: {foo}\u003c/p\u003e\n      \u003cinput value={foo} onChange={(e) =\u003e setFoo(e.target.value)} /\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\n#### useReducerIPC\n\n_Coming soon ..._\n\n### Vue\n\n_Coming soon ..._\n\n### Angular\n\n_Coming soon ..._\n\n## Configuration\n\nIn order for `electron-state-ipc` to work, it must be set up inside the _main process_, _preload script_ and in your render, depending on which UI framework your use.\n\n### Main process\n\nThere is two elements here, we need to:\n\n- Receive updates from all `BrowserWindow`, update the main state, and communicate it to other `BrowserWindow`.\n- Send the state to the new `BrowserWindow` when creating it.\n\nIn practice, you just need to setup the global state:\n\n```ts\nimport { setupGlobalStateIPC } from 'electron-state-ipc';\nimport { BrowserWindow } from 'electron';\n\nsetupGlobalStateIPC();\n\n/* you might set up anything else and create your BrowserWindow(s) */\n...\n\nconst window = new BrowserWindow({\n  ...options, // your options\n});\n```\n\nNote that `setupGlobalStateIPC` return the state itself, if you need to use in in the main process:\n\nIn practice, you just need to setup the global state:\n\n```ts\nimport { setupGlobalStateIPC } from 'electron-state-ipc';\n\nconst state = setupGlobalStateIPC();\n```\n\n### Preload\n\nFor security reasons, this library is built with [context isolation](https://www.electronjs.org/docs/latest/tutorial/context-isolation) in mind.\n\nA single function is needed to set up `electron-state-ipc` inside your **preload** script:\n\n```ts\nimport { exposeStateIPC } from 'electron-state-ipc';\n\nexposeStateIPC();\n```\n\nYou might expose your own APIs, so your preload script might look somewhat like:\n\n```ts\nimport { contextBridge } = from 'electron';\nimport { exposeStateIPC } from 'electron-state-ipc';\n\ncontextBridge.exposeInMainWorld('myAPI', {\n  doAThing: () =\u003e {},\n});\n\nexposeStateIPC();\n```\n\n### Renderer\n\n#### React\n\nAs `electron-state-ipc` works with a context provider. It must be set up, as well as initialized before using any [hook](#usage).\n\nTo be safe, I would render it as a top-level component and wait for it to be initialized, rendering nothing or a loader in the meantime:\n\n```tsx\nimport { ElectronStateIPCContextProvider } from 'electron-state-ipc/react';\n\nReactDOM.render(\n  \u003cElectronStateIPCContextProvider\u003e\n    \u003cApp /\u003e\n  \u003c/ElectronStateIPCContextProvider\u003e,\n  document.getElementById('root'),\n);\n```\n\nAnd in the App component:\n\n```tsx\nimport { useElectronStateIPC } from 'electron-state-ipc/react';\n\nfunction AppProvider() {\n  const state = useElectronStateIPC();\n  if (!state.initialized) return null;\n\n  return (\n    ... // your app: other providers, components, routes ...\n  );\n}\n```\n\n#### Vue\n\n_Coming soon ..._\n\n#### Angular\n\n_Coming soon ..._\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorintd%2Felectron-state-ipc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorintd%2Felectron-state-ipc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorintd%2Felectron-state-ipc/lists"}