@guptasiddhant/react-trigger
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

React Trigger

Publish and listen to changes of Triggers. Implementation of RxJs Subject.

Requires: React ^16.8 for hooks and RxJS.

Trigger

Trigger instances can publish information and subscribe/listen to it anywhere else in the program.

In TypeScript, Pass a PayloadType to let the Trigger know what to expect while observing.

methods

  • publish: Publish (set) new payload
  • subscribe: Subscribe and listen to changes and execute a function.

example

import { Trigger } from "@guptasiddhant/trigger";

// TypeScript
type PayloadType = string;
const myTrigger = new Trigger<PayloadType>();

// JavaScript
const myTrigger = new Trigger();

// somewhere in code
myTrigger.publish("text");

// somewhere else in code
myTrigger.useSubscribe((payload) => console.log(payload));
==or==
useEffect(()=>{
    const sub = myTrigger.subscribe((payload) => console.log(payload))
    return () => sub.unsubscribe();
}

useTrigger

Hook to listen to changes of Triggers (multiple)

params

  • execute: Function to run on Trigger
  • deps: Array of Triggers to listen to.

example

useTrigger((payload) => console.log(payload), [Trigger])

=== or ===

useTrigger((payload) => console.log(payload), [Trigger1, Trigger2])

Readme

Keywords

Package Sidebar

Install

npm i @guptasiddhant/react-trigger

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

12.2 kB

Total Files

7

Last publish

Collaborators

  • guptasiddhant