{"id":13998352,"url":"https://github.com/molefrog/use-leader","last_synced_at":"2025-10-06T18:52:28.185Z","repository":{"id":168393110,"uuid":"607170632","full_name":"molefrog/use-leader","owner":"molefrog","description":"🙋‍♀️ React hook for cross-tab leader election","archived":false,"fork":false,"pushed_at":"2023-05-23T11:03:28.000Z","size":154,"stargazers_count":17,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-21T08:53:08.428Z","etag":null,"topics":["distributed-systems","hook","leader","leader-election","reactjs"],"latest_commit_sha":null,"homepage":"https://use-leader.surge.sh","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/molefrog.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-02-27T13:13:31.000Z","updated_at":"2024-11-29T00:00:12.000Z","dependencies_parsed_at":"2024-01-15T19:44:30.512Z","dependency_job_id":"9c878204-d0b8-4c6f-a969-da570885d6b9","html_url":"https://github.com/molefrog/use-leader","commit_stats":null,"previous_names":["molefrog/use-leader"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/molefrog/use-leader","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/molefrog%2Fuse-leader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/molefrog%2Fuse-leader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/molefrog%2Fuse-leader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/molefrog%2Fuse-leader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/molefrog","download_url":"https://codeload.github.com/molefrog/use-leader/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/molefrog%2Fuse-leader/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278663381,"owners_count":26024389,"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","status":"online","status_checked_at":"2025-10-06T02:00:05.630Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["distributed-systems","hook","leader","leader-election","reactjs"],"created_at":"2024-08-09T19:01:36.345Z","updated_at":"2025-10-06T18:52:28.149Z","avatar_url":"https://github.com/molefrog.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"```js\r\n/**\r\n *  `useLeader()` – React hook that checks whether the current tab is the primary one.\r\n *  works over `BroadcastChannel` and uses slightly modified \"Bully\" leader election.\r\n */\r\n\r\n// simple usage\r\nimport { useLeader } from \"use-leader\"\r\nconst { isLeader } = useLeader() // true/false\r\n\r\n// alias, provides boolean value only\r\nimport { useIsLeader } from \"use-leader\"\r\nconst isLeader = useIsLeader() // true/false\r\n\r\n// additional state \r\nconst { \r\n  id,       // id of the current node (tab)\r\n  leaderId, // id of the elected leader\r\n  ref       // reference to election instance, can be used for imperative control\r\n            // over the state of the node e.g. `ref.lead()` - force become a leader\r\n} = useLeader()\r\n\r\n// each node has a unique identifier, when the leader is elected, every node holds \r\n// the leading id. these ids are randomly generated... so why would you want \r\n// to override it?\r\nconst { isLeader } = useLeader({ id: 'my1d' })\r\n\r\n// the answer is instance sharing. if your app has two places where `useLeader()` \r\n// is called, they both have access to the same node instance.\r\n\r\n\u003cCompOne /\u003e   // useLeader() - true, id = \"abc\"\r\n\u003cCompTwo /\u003e   // useLeader() - true, id = \"abc\"\r\n\u003cCompThree /\u003e // useLeader({ id: \"efg\" }) - false, id = \"efg\"\r\n\r\n// when all components that rely on `useLeader()` exit the tree, the node shuts down,\r\n// meaning that the other tabs now can take the advantage and relect the leader\r\n\r\n// \"totalitarian\" mode (`true` by default) means that when a new node enters the quorum, \r\n// the existing leader won't give up power even if the new node's id is higher.\r\n\r\nuseLeader({ totalitarian: false })\r\n\r\n// see website https://use-leader.surge.sh/ for interactive demos\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmolefrog%2Fuse-leader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmolefrog%2Fuse-leader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmolefrog%2Fuse-leader/lists"}