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-action-plug

v0.0.8

Published

A React library for pluggable action interface between components

Downloads

11

Readme

react-action-plug

npm version

A React library for pluggable action interface between components.

Motivation

If you are developing a complex GUI screen in React, you may want to call an interaction or action in one component from multiple components. This library provides an action plug that can be used between components.

Features

  • TypeScript support
  • Zero dependency

Installation

yarn add react-action-plug

Usage

import React from 'react';
import {createActionPlug} from 'react-action-plug';

// Define action plug
type MyActions = {
  increment(n: number): void;
  reset(): void;
};
const {Boundary, useActionHandlers, useActions} = createActionPlug<MyActions>();

function Counter() {
  const [count, setCount] = React.useState(0);
  // Prepare action handlers to do something
  useActionHandlers({
    increment(n: number) {
      setCount(count => count + n);
    },
    reset() {
      setCount(0);
    }
  });

  return (
    <div>count: {count}</div>
  );
}

function Controller() {
  const {increment, reset} = useActions();
  return (
    <>
      {/* Trigger "increment" action when click */}
      <button onClick={() => increment(1)}>click!</button>
  
      {/* Trigger "reset" action when click */}
      <button onClick={() => reset()}>reset</button>
    </>
  );
}

function App() {
  // Wrap with Boundary component, which specifies the scope of the actions and those handlers.
  return <Boundary>
    <Controller />
    <Counter />
  </Boundary>;
}

API

createActionPlug<T = {[actionName: string]: (payload: any) => void>()

  • <T> - Type for the actions.
type Actions = {
  increment(): void;
}
const {Boundary, useActions, useActionHandlers} = createActionPlug<Actions>();

useActionHandlers(handlers: Partial)

Prepare a handler functions for the action plug.

function MyComponent() {
  const [state, setState] = useState(0);
  useActionHandlers({
    increment() {
      setState(count => count + 1);
    }
  });

  return <div>count: {state}</div>;
}

useActions()

Get a function to trigger action plug with something payload.

function MyController() {
  const actions = useActions();
  const handlerClick = () => {
    actions.increment();
  };

  return <div onClick={handleClick}>increment</div>;
}

Boundary: React.FC<{children: React.ReactNode}>

Create a boundary for action plug trigger to be enabled.

const {Boundary, useActions, useActionHandlers} = createActionPlug<{/* ... */}>();

function Container() {
  return (
    <Boundary>
      <Controller />
      <Counter />
    </Boundary>
  );
}

License

MIT License

Author

Kubota Mitsunori, @anatoo