npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-dispatch

v1.2.0

Published

This is a Event Emitter Class like util function. Just like Node.js EventEmitter, you can communicate through different components in your application.

Downloads

47

Readme

React Dispatcher Util Library

A light weight🎆, extremely fast and efficient event emitter class written in TypeScript, for sending data up and down through components🔥

This is a Event Emitter Class like util function. Just like Node.js EventEmitter, you can communicate through different components in your application. But we use dispatch and subscribe, like subscribe pattern, instead of on, emit in Node.js world.

Checkout this demo : Demo

Background

In React or Vue, we all know how to pass props to manage data flow inside our application. But sometimes, we have brother components that they are not inside one another. This situation we usually would use some data management library like Redux or Context Api. But that would take longer time and more codes to set up. React dispatcher is made for this. Its pretty handy if you deal with shared data in different files. It lets developer easily communicate through components, making to all connected!

Installation

npm install react-dispatch

or

yarn add react-dispatch

Then 4 main functions...

dispatch(string, data:any) dispatch an action, it will send whatever data you defined to subscribe function.

on(string, () => {}) subscribe to an action, when an action got dispatched, this function will run

once(string, () => {}) subscribe only once, similar to EventEmitter.once function in Node.js. The listener will be destroyed after first action get dispatched.

off(string | string[]) used to clear the memory when done

Example

import React,{ useState } from 'react'
import { dispatcher } from 'react-dispatch'

const UPDATE = 'update' // good to import constant file outside

const App  = () => {

    const onClick = () => dispatcher.dispatch(UPDATE, 1)

    return(
       <button onClick={onClick}>dispatch Me.</button>
    )
  
}
import React,{ useState, useEffect } from 'react'
import { dispatcher } from 'react-dispatch'

const UPDATE = 'update' // good to import constant file outside
const AppBrotherComponent = () => {
    const [count, setCount] = useState(0)

    useEffect(() => {
        dispatcher.on(UPDATE, res => setCount(count + res));
        // whenever it receives a dispatch, it will fire the callback. 
        return () => {
            dispatcher.off(UPDATE)
        }
    },[])

    return (
        <p>
        Data received from dispatch: {data}
        </p>
    )
}

Provided API

dispatcher.dispatch dispatch function takes string as its first argument, the data you want the subscribe function to receive is the second argument.

dispatcher.on The first parameter takes EXACT same text you write in dispatch function to be able to match. The second parameter is the callback function that you do with the data from the dispatch.

dispatcher.once This is similar to on function, whats different is it only gets called once. It will not work if you want to fire it multiple times.

dispatcher.off This is usually used when component unmounted, and recycle the memory in case of memory leak in your application. Exp. Use in ComponentWillUnmount, etc.. It takes action you dispatched, it could be one action or array of actions.

Better improvement?

Suggestions or issues, please open an issue on github