All Projects → fabienjuif → use-bus

fabienjuif / use-bus

Licence: MIT License
React hook to subscribe and dispatch events accros React components

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to use-bus

telephone-ts
Telephone-ts: The "Event Emitter-less" TypeScript Event Architecture.
Stars: ✭ 22 (-56.86%)
Mutual labels:  events, eventbus, message
Multiplatform-Bus
Kotlin event-bus compatible with Android & native iOS
Stars: ✭ 43 (-15.69%)
Mutual labels:  bus, eventbus, message
use-scroll-direction
A simple, performant, and cross-browser hook for detecting scroll direction in your next react app.
Stars: ✭ 24 (-52.94%)
Mutual labels:  hooks, hook
yii2-message
System for users to send each other private messages.
Stars: ✭ 37 (-27.45%)
Mutual labels:  message, messages
transition-hook
☄️ An extremely light-weight react transition animation hook which is simpler and easier to use than react-transition-group
Stars: ✭ 250 (+390.2%)
Mutual labels:  hooks, hook
use-double-tap
React hook for handling double tap on mobile devices
Stars: ✭ 18 (-64.71%)
Mutual labels:  hooks, hook
Alpine
Basic event system framework using functional interfaces
Stars: ✭ 79 (+54.9%)
Mutual labels:  events, eventbus
react-use-observer
Performant react hooks for WebApi Observers, useResizeObserver, useInteractionObserver, useMutationObserver
Stars: ✭ 19 (-62.75%)
Mutual labels:  hooks, hook
react-ui-hooks
🧩Simple repository of React hooks for building UI components
Stars: ✭ 20 (-60.78%)
Mutual labels:  hooks, hook
entangle
Global state management tool for react hooks inspired by RecoilJS and Jotai using proxies.
Stars: ✭ 26 (-49.02%)
Mutual labels:  hooks, hook
e
A library which combines a eventBus/emitter, DOM events management, delegated events, and event-based utils into a single lightweight and performant library.
Stars: ✭ 37 (-27.45%)
Mutual labels:  events, eventbus
use-smooth-scroll
React hook which gives a smooth scrolling function.
Stars: ✭ 41 (-19.61%)
Mutual labels:  hooks, hook
crypto-watchdog
Crypto Watchdog is an open-source developer friendly project, periodically queries crypto market and notifies potential pumps & recently added tokens/coins via web-hooks.
Stars: ✭ 22 (-56.86%)
Mutual labels:  hooks, hook
react-use-hoverintent
React hook for hoverIntent
Stars: ✭ 16 (-68.63%)
Mutual labels:  hooks, hook
pg-pubsub
Reliable PostgreSQL LISTEN/NOTIFY with inter-process lock support
Stars: ✭ 50 (-1.96%)
Mutual labels:  events, eventbus
hertzy
Event bus channel
Stars: ✭ 48 (-5.88%)
Mutual labels:  events, eventbus
vue-happy-bus
Event Bus for vue-next, automatically cancel listening events when unmounted. 基于 vue3 的 event bus,带有自动销毁事件功能。
Stars: ✭ 99 (+94.12%)
Mutual labels:  events, bus
SimpleDialogs
💬 A simple framework to help displaying dialogs on a WPF app
Stars: ✭ 24 (-52.94%)
Mutual labels:  message, messages
StaticBus
🚌 A static bus use in modules.
Stars: ✭ 15 (-70.59%)
Mutual labels:  bus, eventbus
ReactiveBus
🚍 Reactive Event Bus for JVM (1.7+) and Android apps built with RxJava 2
Stars: ✭ 17 (-66.67%)
Mutual labels:  bus, eventbus

use-bus

React hook to subscribe and dispatch events accros React components

npm npm bundle size CircleCI Coveralls github

API

dispatch

import { dispatch } from 'use-bus':

  • dispatch('string'): will dispatch the action { type: 'string' } without payload
  • dispatch({ type: 'string', payload: 3 }): will dispatch the given action

useBus

import useBus from 'use-bus':

  • useBus(filter, callback, deps): register the given callback to the given filter
    • filter: it can be a string or a function
      • string: if filter is a string, then the action type is test over this given string, if the filter match the type, the callback is called
      • function: the callback is called if the function returns a truthy value
    • callback: take the action as the first argument so you can retrieve its type and its payload for example
    • deps: is an array where you declare variables you use in callback, like you are doing for a useEffect from React

Example

register to an event (and react to it)

import React, { useState } from 'react'
import useBus from 'use-bus'

const PrintIterations = () => {
  const [iterations, setIterations] = useState(0)

  useBus(
    '@@ui/ADD_ITERATION',
    () => setIterations(iterations + 1),
    [iterations],
  )

  return (
    <div>
      {'There is '}
      {iterations}
      {' iterations'}
    </div>
  )
}

export default PrintIterations
  1. import the hook useBus
  2. register to an event name, here @@ui/ADD_ITERATION
  3. react to this, here an anonymous function that increment a number

dispatch an event

import React from 'react'
import { dispatch } from 'use-bus'

const IterateBtn = () => {
  return (
    <button onClick={() => dispatch('@@ui/ADD_ITERATION')}>
      Iterate
    </button>
  )
}

export default IterateBtn
  1. import dispatch and call it with the event you want to send

Connect the dispatcher and the reaction

import React from 'react'
import PrintIterations from './printIterations'
import IterateBtn from './iterateBtn'

const App = () => {
  return (
    <div>
      <PrintIterations />
      <IterateBtn />
    </div>
  )
}

export default App

There is no connection to do, this is already done by use-bus.

This example just demonstrate that siblings can interact, but you can imagine a dispatcher wherever you want in the React tree and something that react to the dispatch wherever you want to.

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].