All Projects → goto-bus-stop → react-bus

goto-bus-stop / react-bus

Licence: MIT License
A global event emitter for react.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-bus

micro-typed-events
The smallest, most convenient typesafe TS event emitter you'll ever need
Stars: ✭ 39 (+14.71%)
Mutual labels:  pubsub, event-emitter
shamash
Autoscaling for Google Cloud Dataproc
Stars: ✭ 31 (-8.82%)
Mutual labels:  pubsub
telephone-ts
Telephone-ts: The "Event Emitter-less" TypeScript Event Architecture.
Stars: ✭ 22 (-35.29%)
Mutual labels:  event-emitter
sol
Lightweight MQTT broker, written from scratch. IO is handled by a super simple event loop based upon the most common IO multiplexing implementations.
Stars: ✭ 72 (+111.76%)
Mutual labels:  pubsub
libmcu
A toolkit for firmware development
Stars: ✭ 33 (-2.94%)
Mutual labels:  pubsub
liftbridge-api
Protobuf definitions for the Liftbridge gRPC API. https://github.com/liftbridge-io/liftbridge
Stars: ✭ 15 (-55.88%)
Mutual labels:  pubsub
celery-connectors
Want to handle 100,000 messages in 90 seconds? Celery and Kombu are that awesome - Multiple publisher-subscriber demos for processing json or pickled messages from Redis, RabbitMQ or AWS SQS. Includes Kombu message processors using native Producer and Consumer classes as well as ConsumerProducerMixin workers for relay publish-hook or caching
Stars: ✭ 37 (+8.82%)
Mutual labels:  pubsub
events
Tiny type-safe event emitter
Stars: ✭ 25 (-26.47%)
Mutual labels:  event-emitter
typed-emitter
🔩 Type-safe event emitter interface for TypeScript
Stars: ✭ 175 (+414.71%)
Mutual labels:  event-emitter
megaphone
Hear ye, hear ye 📣
Stars: ✭ 15 (-55.88%)
Mutual labels:  pubsub
node-await-event-emitter
await events library like EventEmitter
Stars: ✭ 19 (-44.12%)
Mutual labels:  event-emitter
flask-redis-realtime-chat
A simple Flask realtime chat using Redis PubSub
Stars: ✭ 31 (-8.82%)
Mutual labels:  pubsub
build-your-own-platform-with-knative
Knativeのコンポーネントを理解しながらFaaSプラットフォームをDIYするワークショップです
Stars: ✭ 43 (+26.47%)
Mutual labels:  pubsub
go-aws-msg
AWS Pub/Sub Primitives for Go
Stars: ✭ 22 (-35.29%)
Mutual labels:  pubsub
svelte-local-storage-store
Adds pub/sub to local storage.
Stars: ✭ 194 (+470.59%)
Mutual labels:  pubsub
subee
✉️ 🐝 It's not only a bee, but a message - Pub/Sub Worker Framework Implementation
Stars: ✭ 40 (+17.65%)
Mutual labels:  pubsub
js-libp2p-gossipsub
JavaScript implementation of Gossipsub
Stars: ✭ 76 (+123.53%)
Mutual labels:  pubsub
mros2
agent-less and lightweight communication library compatible with rclcpp for embedded devices
Stars: ✭ 72 (+111.76%)
Mutual labels:  pubsub
opal
Policy and data administration, distribution, and real-time updates on top of Open Policy Agent
Stars: ✭ 459 (+1250%)
Mutual labels:  pubsub
httpubsub
Basic Pub/Sub over HTTP/S
Stars: ✭ 22 (-35.29%)
Mutual labels:  pubsub

react-bus

npm bundlephobia

A global event emitter for React apps. Useful 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).

Usage

react-bus contains a <Provider /> component and a useBus hook.

<Provider /> creates an event emitter and places it on the context. useBus() returns the event emitter from context.

import { Provider, useBus } from 'react-bus'
// Use `bus` in <Component />.
function ConnectedComponent () {
  const bus = useBus()
}

<Provider>
  <ConnectedComponent />
</Provider>

For example, to communicate "horizontally" between otherwise unrelated components:

import { Provider as BusProvider, useBus, useListener } from 'react-bus'
const App = () => (
  <BusProvider>
    <ScrollBox />
    <Input />
  </BusProvider>
)

function ScrollBox () {
  const el = React.useRef(null)
  const onscroll = React.useCallback(function (top) {
    el.current.scrollTop += top
  }, [])

  useListener('scroll', onscroll)

  return <div ref={el}></div>
}

// Scroll the ScrollBox when pageup/pagedown are pressed.
function Input () {
  const bus = useBus()
  return <input onKeyDown={onkeydown} />

  function onkeydown (event) {
    if (event.key === 'PageUp') bus.emit('scroll', -200)
    if (event.key === 'PageDown') bus.emit('scroll', +200)
  }
}

This may be easier to implement and understand than lifting the scroll state up into a global store.

Install

npm install react-bus

API

<Provider />

Create an event emitter that will be available to all deeply nested child elements using the useBus() hook.

const bus = useBus()

Return the event emitter.

useListener(name, fn)

Attach an event listener to the bus while this component is mounted. Adds the listener after mount, and removes it before unmount.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].