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

@codora/react-topic

v1.0.1

Published

React component and hook to publish and subscribe to topics.

Downloads

1

Readme

CodoraDev - React Topic

A react hook to publish and subscribe to a topic from any component.

version npm downloads

Report Bug - Request Feature

✨ Install

npm install --save @codora/react-topic

👉 Import

import {Topic, useTopic} from "@codora/react-topic";

🚀 Demo

Coming Soon...

🔬 Use Topic

Add the <Topic> context any where in your app. Add it near the root if you want to use topics for your whole app.

import {Topic} from "@codora/react-topic";

const App = () => {
  return (
    <Topic>
      //...
    </Topic>
  )
}

You will then have access to useTopic. It takes one argument, which will be the topic name, and one type argument, which will be the topic type.

useTopic will return two functions: publish and subscribe

import {useTopic} from "@codora/react-topic";

type User = {
    name
}
const SomeComponent = () => {
    const {publish, subscribe} =useTopic<User>("user")

    //...

publish is quite simple. All you need to do is pass in the data. This will add the data to its topic collection and send out an event to any subscribers.

//...

const newUser: User = buildUser();

<button onclick={()=> 
    publish(newUser) 
}>
    Add User
</button>

//...

subscribe will have to be used with in a useEffect for it to stay open and subscribed. subscribe will return an unSubscribe function which should be called when ever you do not need the subscription, else there will be a memory leak in your application.

The first argument is an onSubscribe function which is the callback function that will be called on an event. onSubscribe as two input arguments. The first is the topic updated collection array. We pass whole topic array with each event as it feels the most natural when making web applications. The second input argument is the event that was sent. In this event will hold the individual topic.

//...

  const [users, setUsers] = useState<User[]>([])
  const [event, setEvent] = useState<Event<User>>()

  useEffect(() => {
    const unSubscribe = subscribe((users, event) => {
      setUsers(users)
      setEvent(event)
    })
    return () => {
      unSubscribe()
    }
  }, []) 

//...

A full example is available here.

🦄 Upcoming Features

@codoradev/react-topic has all the potentials to grow further. Here are some of the upcoming features planned(not in any order),

  • On the first subscribe you can choose which which event to replay from

🛡️ License

This project is licensed under the MIT License - see the LICENSE file for details.