{"id":22594709,"url":"https://github.com/nucleoidai/react-event","last_synced_at":"2025-04-11T00:14:38.874Z","repository":{"id":47601692,"uuid":"486774117","full_name":"NucleoidAI/react-event","owner":"NucleoidAI","description":"Event-driven Alternative to React Context","archived":false,"fork":false,"pushed_at":"2024-12-09T10:35:05.000Z","size":1778,"stargazers_count":28,"open_issues_count":5,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-11T00:14:29.410Z","etag":null,"topics":["event-driven","javascript","react"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NucleoidAI.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-28T23:10:57.000Z","updated_at":"2025-03-23T09:59:26.000Z","dependencies_parsed_at":"2024-04-17T21:41:58.699Z","dependency_job_id":"99401538-a550-40fd-8f1b-fcfa06aecf78","html_url":"https://github.com/NucleoidAI/react-event","commit_stats":null,"previous_names":["nucleoidjs/react-events","nucleoidjs/synapses","nucleoidjs/react-event","nucleoidai/react-event"],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NucleoidAI%2Freact-event","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NucleoidAI%2Freact-event/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NucleoidAI%2Freact-event/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NucleoidAI%2Freact-event/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NucleoidAI","download_url":"https://codeload.github.com/NucleoidAI/react-event/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248317730,"owners_count":21083530,"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":["event-driven","javascript","react"],"created_at":"2024-12-08T10:07:19.696Z","updated_at":"2025-04-11T00:14:38.851Z","avatar_url":"https://github.com/NucleoidAI.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003ereact-event\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.apache.org/licenses/LICENSE-2.0\"\u003e\u003cimg src=\"https://img.shields.io/badge/Apache-2.0-yellow?style=for-the-badge\u0026logo=apache\" alt=\"License\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/@nucleoidai/react-event\"\u003e\u003cimg src=\"https://img.shields.io/badge/NPM-red?style=for-the-badge\u0026logo=npm\" alt=\"NPM\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://discord.com/invite/eWXFCCuU5y\"\u003e\u003cimg src=\"https://img.shields.io/badge/Discord-lightgrey?style=for-the-badge\u0026logo=discord\" alt=\"Discord\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n![Banner](.github/media/react-event-banner.png)\n\n\u003cp align=\"center\"\u003e\n  Event-driven Alternative to React Context\n\u003c/p\u003e\n\n\u003cbr/\u003e\n\n```shell\nnpm i @nucleoidai/react-event\n```\n\nreact-event is an alternative to React Context with event-driven style communication that helps to build loosely coupled components.\n\n### How it works?\n\nSubscribers are registered an event with the custom hook `useEvent(eventType, initialValue)`, once publisher posts an event and its payload, react-event asynchronously sends the event to subscribed components and subscribed components will eventually be re-rendered with fresh data.\n\nExample:\n\n```javascript\nimport { useEvent } from \"@nucleoidai/react-event\";\n\nconst Component1 = () =\u003e {\n  const [event] = useEvent(\"BUTTON_CLICKED\", { number: 10 });\n\n  return \u003cdiv\u003e{event.number}\u003c/div\u003e;\n};\n```\n\n```javascript\nimport { useEvent } from \"@nucleoidai/react-event\";\n\nconst Component2 = () =\u003e {\n  const [event] = useEvent(\"BUTTON_CLICKED\", { string: \"blue\" });\n\n  return \u003cdiv\u003e{event.string}\u003c/div\u003e;\n};\n```\n\n```javascript\nimport { publish } from \"@nucleoidai/react-event\";\n\nconst PublishComponent = () =\u003e {\n  return (\n      \u003cbutton\n          onClick={() =\u003e {\n            publish(\"BUTTON_CLICKED\", { number: 11, string: \"red\" });\n          }}\n      \u003e\n        Button\n      \u003c/button\u003e\n  );\n};\n```\n\n\u003cimg src=\".github/media/sample.gif\" alt=\"Sample react-event\" width=\"350\" /\u003e\n\nThe complete sample project is [here](./sample).\n\n### Stateless Handling\n\nreact-event supports stateless components with caching last published payload for the event type, so that if the component is re-rendered, it won't lose the payload. For example, Component 3 in this example is not re-rendered yet, but react-event holds the last payload for the event type, and once the component is rendered, it returns the payload instead of initial value.\n\n\u003cimg src=\".github/media/react-event.drawio.png\" alt=\"react-event Diagram\" width=\"350\" /\u003e\n\n## Event-driven Architecture\n\nEvent-driven Architecture is commonly used in Microservices systems that pretty much targets similar problem; loose coupling. This style of architecture require middleware like Kafka, RabbitMQ etc. and we are trying to adopt the very same idea to React.js, of course with some modification such as \"Stateless Handling\".\n\n## API\n\n#### `const [ event ] = useEvent ( eventType , initialValue )`\n\nReact Hook is to subscribe an event. If there is no event posted yet, it returns `initialValue`, otherwise, returns last published payload for the event type from cache.\n\n#### `publish ( eventType, payload )`\n\nPublish function to post ane event and its payload.\n\n#### `subscribe ( type , callback )`\n\nSubscribe function acts like `useEvent` for non-React JavaScript.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnucleoidai%2Freact-event","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnucleoidai%2Freact-event","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnucleoidai%2Freact-event/lists"}