{"id":15765125,"url":"https://github.com/huozhi/react-horn","last_synced_at":"2026-05-10T06:54:45.967Z","repository":{"id":74389753,"uuid":"186201380","full_name":"huozhi/react-horn","owner":"huozhi","description":"cross component communication with actions","archived":false,"fork":false,"pushed_at":"2020-06-21T15:00:09.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-14T07:20:40.105Z","etag":null,"topics":["actions","broadcast","cross-component","dispatch","event","react"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/huozhi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-12T02:18:03.000Z","updated_at":"2020-06-21T15:00:12.000Z","dependencies_parsed_at":"2023-04-28T14:01:33.891Z","dependency_job_id":null,"html_url":"https://github.com/huozhi/react-horn","commit_stats":{"total_commits":10,"total_committers":1,"mean_commits":10.0,"dds":0.0,"last_synced_commit":"9122422529e936d381f898005d6da384d16a1214"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huozhi%2Freact-horn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huozhi%2Freact-horn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huozhi%2Freact-horn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huozhi%2Freact-horn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/huozhi","download_url":"https://codeload.github.com/huozhi/react-horn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246453246,"owners_count":20779923,"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":["actions","broadcast","cross-component","dispatch","event","react"],"created_at":"2024-10-04T12:24:41.345Z","updated_at":"2026-05-10T06:54:40.918Z","avatar_url":"https://github.com/huozhi.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-horn\n\u003e cross component communication with actions\n\n## Why?\n\nReact works based on state/props changes. Corss communication may let you hoist some state to global state like root component state or in redux store. react-horn wants to give user ability to capture the state mutation rather than the final state. It works like event listeners but the listeners are smart in auto subscribing events.\n\n## API\n\n### \u003cHorn.Provider event={event: Event}\u003e\n\nWrap the `Provider` on your component to make it avaible for broadcast events for any nested component wrapped by `withHorn` and functional component using `useHorn`\n\n`event` is Node.js standard event like instance, at least require it to have `on`, `off`, `emit` 3 methods.\nyou can create your own event which holding these 3 methods, and pass it to `Horn.Provider`.\n\n### withHorn\n\nHOC `withHorn` provide descendants to fire actions with type and payload to the subscribers.\naction scheme is similiar to [FSA](https://github.com/redux-utilities/flux-standard-action).\n\n`{type: string, payload: any?}`\n\n### useHorn\n\nReact hooks API has same function comparing to `withHorn`.\n\n\n## Usage\n\n```js\nimport EventEmitter from \"events\"\nimport Horn, { withHorn } from \"react-horn\";\nimport { useHorn } from \"react-horn/hooks\";\n\nconst Dialer = () =\u003e {\n  function onAck(action) {\n    console.log(action.type + \":\", action.payload); // ack: 2\n  }\n\n  const emit = useHorn();\n  return (\n    \u003cbutton onClick={() =\u003e emit(\"syn\", 1)}\u003e\n      syn\n      \u003cHorn.Subscriber on={\"ack\"} handler={onAck} /\u003e\n    \u003c/button\u003e\n  );\n};\n\nconst Feedback = withHorn(({ emit }) =\u003e {\n  return (\n    \u003cHorn.Subscriber on={\"syn\"} handler={() =\u003e emit(\"ack\", 2)} /\u003e\n  );\n});\n\nconst event = new EventEmitter();\n\nrender(\n  \u003cHorn.Provider event={event}\u003e\n    \u003cDialer /\u003e\n    \u003cFeedback /\u003e\n  \u003c/Horn.Provider\u003e,\n  document.getElementById(\"root\")\n);\n\nevent.emit('syn');\n```\n\n## Playground\n\n[codesandbox example](https://codesandbox.io/s/njm193jr)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuozhi%2Freact-horn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhuozhi%2Freact-horn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuozhi%2Freact-horn/lists"}