{"id":15395051,"url":"https://github.com/goto-bus-stop/react-bus","last_synced_at":"2025-04-06T02:08:04.020Z","repository":{"id":27594616,"uuid":"114534075","full_name":"goto-bus-stop/react-bus","owner":"goto-bus-stop","description":"A global event emitter for react.","archived":false,"fork":false,"pushed_at":"2025-03-24T05:14:35.000Z","size":46,"stargazers_count":42,"open_issues_count":2,"forks_count":6,"subscribers_count":3,"default_branch":"default","last_synced_at":"2025-03-30T01:07:41.420Z","etag":null,"topics":["event-emitter","pubsub","react"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/react-bus","language":"JavaScript","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/goto-bus-stop.png","metadata":{"files":{"readme":"readme.md","changelog":"CHANGELOG.md","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":"2017-12-17T12:25:02.000Z","updated_at":"2025-02-06T18:00:26.000Z","dependencies_parsed_at":"2024-06-18T18:39:00.390Z","dependency_job_id":"67068653-14a3-4cfa-9d85-c884eb1311dc","html_url":"https://github.com/goto-bus-stop/react-bus","commit_stats":{"total_commits":49,"total_committers":4,"mean_commits":12.25,"dds":0.1428571428571429,"last_synced_commit":"53a4f3072ffbdf6e98bbab4426a99268bfb6791b"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goto-bus-stop%2Freact-bus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goto-bus-stop%2Freact-bus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goto-bus-stop%2Freact-bus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goto-bus-stop%2Freact-bus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/goto-bus-stop","download_url":"https://codeload.github.com/goto-bus-stop/react-bus/tar.gz/refs/heads/default","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247423513,"owners_count":20936626,"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-emitter","pubsub","react"],"created_at":"2024-10-01T15:25:29.384Z","updated_at":"2025-04-06T02:08:04.002Z","avatar_url":"https://github.com/goto-bus-stop.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-bus\n\n[![npm](https://badgen.net/npm/v/react-bus)](https://npmjs.com/package/react-bus)\n[![bundlephobia](https://badgen.net/bundlephobia/minzip/react-bus)](https://bundlephobia.com/result?p=react-bus)\n\nA global [event emitter](https://github.com/developit/mitt) for React apps.\nUseful if you need some user interaction in one place trigger an action in another place on the page, such as scrolling a logging element when pressing PageUp/PageDown in an input element (without having to store scroll position in state).\n\n## Usage\n\nreact-bus contains a `\u003cProvider /\u003e` component and a `useBus` hook.\n\n`\u003cProvider /\u003e` creates an event emitter and places it on the context.\n`useBus()` returns the event emitter from context.\n\n```js\nimport { Provider, useBus } from 'react-bus'\n// Use `bus` in \u003cComponent /\u003e.\nfunction ConnectedComponent () {\n  const bus = useBus()\n}\n\n\u003cProvider\u003e\n  \u003cConnectedComponent /\u003e\n\u003c/Provider\u003e\n```\n\nFor example, to communicate \"horizontally\" between otherwise unrelated components:\n\n```js\nimport { Provider as BusProvider, useBus, useListener } from 'react-bus'\nconst App = () =\u003e (\n  \u003cBusProvider\u003e\n    \u003cScrollBox /\u003e\n    \u003cInput /\u003e\n  \u003c/BusProvider\u003e\n)\n\nfunction ScrollBox () {\n  const el = React.useRef(null)\n  const onscroll = React.useCallback(function (top) {\n    el.current.scrollTop += top\n  }, [])\n\n  useListener('scroll', onscroll)\n\n  return \u003cdiv ref={el}\u003e\u003c/div\u003e\n}\n\n// Scroll the ScrollBox when pageup/pagedown are pressed.\nfunction Input () {\n  const bus = useBus()\n  return \u003cinput onKeyDown={onkeydown} /\u003e\n\n  function onkeydown (event) {\n    if (event.key === 'PageUp') bus.emit('scroll', -200)\n    if (event.key === 'PageDown') bus.emit('scroll', +200)\n  }\n}\n```\n\nThis may be easier to implement and understand than lifting the scroll state up into a global store.\n\n## Install\n\n```\nnpm install react-bus\n```\n\n## API\n\n### `\u003cProvider /\u003e`\n\nCreate an event emitter that will be available to all deeply nested child elements using the `useBus()` hook.\n\n### `const bus = useBus()`\n\nReturn the event emitter.\n\n### `useListener(name, fn)`\n\nAttach an event listener to the bus while this component is mounted. Adds the listener _after_ mount, and removes it before unmount.\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoto-bus-stop%2Freact-bus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoto-bus-stop%2Freact-bus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoto-bus-stop%2Freact-bus/lists"}