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-simple-events

v1.0.1

Published

Simple easy events for React that gets out of your way.

Downloads

4

Readme

react-simple-events

minzipped size dependencies tree shaking

react-simple-events is the simplest, smallest, easiest, most idiomatic, and just plain great way to introduce events into your React application

Getting Started

Install react-simple-events, and then write some code!

pnpm add react-simple-events
yarn add react-simple-events
npm i react-simple-events
import React, { useState } from "react";
import { createNanoEvents, createUseEvent } from "react-simple-events";

interface UpdateEvents {
  notify(message: string): void;
}

const updateEmitter = createNanoEvents<UpdateEvents>();
const useUpdateEvent = createUseEvent(updateEmitter);

function Component() {
  const [message, setMessage] = useState("");
  useUpdateEvent("notify", (message) => setMessage(message));
  return <p>Message: {message}</p>;
}

updateEmitter.emit("notify", "Hello, world!");

Why Events?

In React, how would you approach the following?

  • Efficient and granular updates in a large tree of components
  • Easily querying React state from outside React
  • Two-way communication between global and React state

react-simple-events is the answer. Coming in at a small < 1000 byte footprint, it can solve these problems and more using events:

  • Emit events when something happens, and have your React components listen and only update on that event if they need to
  • Write event handlers in React components that return values
  • Use event emitting and RPC (a thin layer over events) to cross the boundaries

react-simple-events was born out of a need, and has solved real problems:

  • Smiley Face Game uses React for UI and PixiJS for rendering a game. Events are the answer to every single question - from asking for user input, have UI pop up on user actions, React and global variables are in harmony thanks to events.
  • Potentially you! Why not submit a PR if you're using react-simple-events?

Features

  • Small in size — under 1000 bytes minified + gzipped!
  • Simple in nature — complexity is the enemy.
  • React.Context API — usable from React Contexts if you don't like global variables.
  • Versatile — the event-driven architecture has many uses!

Examples

Gallery: Efficient, granular updating

A good usage of events is for efficient, granular updating is when displaying a large amount of items.

The following demo could have tons of items, with complex interactivity rules, and only the bare minimum will need to be rendered thanks to the usage of events.

Gallery

Click to view source!

Passcode: Querying State from React

Another good usage of events is for being able to query values from React, and use them elsewhere. This functionality is useful for communication between two far-away parts of your application. For example, a game that asks for the user to enter a password upon entering a trigger zone by showing React UI.

In the following demo, the RPC functionality of react-simple-events is used to showcase querying values from React UI while game logic that's completely separate from React is happening.

Passcode

Click to view source!