Skip to content

Latest commit

Β 

History

History
56 lines (39 loc) Β· 1.21 KB

File metadata and controls

56 lines (39 loc) Β· 1.21 KB

react global event bus

Global event bus hook for react

GitHub Actions Status NPM gzip

πŸ”§ Installation

npm install --save react-gbus

✨ Features

  • πŸ“’ Broadcast(emit) from anywhere (not limited to react components)
  • πŸƒ Ultra lightweight less then 1KB
  • ✌ Written w/ TypeScript

πŸ“¦ Usage

import React, { useState } from "react";

import BusProvider, { useListener, emit } from "react-gbus";

const FRUIT_CHANGE = "fch";

function FruitViewer() {
  const [Fruit, setFruit] = useState("Choose your Fruit");

  useListener(setFruit, [FRUIT_CHANGE]);

  return <h1>{Fruit}</h1>;
}

const FruitChooser = () => (
  <div>
    <button onClick={() => emit(FRUIT_CHANGE, "🍎")}>Apple</button>
    <button onClick={() => emit(FRUIT_CHANGE, "πŸ₯­")}>Mango</button>
    <button onClick={() => emit(FRUIT_CHANGE, "🍍")}>Pineapple</button>
  </div>
);

const App = () => (
  <BusProvider>
    <FruitViewer />
    <FruitChooser />
  </BusProvider>
);

License

MIT Β© harshzalavadiya