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-hook-chain

v0.0.1

Published

A lightweight and flexible framework for building task execution chains in React applications. With `react-hook-chain`, you can define, manage, and execute sequential tasks (responsibility chains) with ease.

Downloads

75

Readme

react-hook-chain

A lightweight and flexible framework for building task execution chains in React applications. With react-hook-chain, you can define, manage, and execute sequential tasks (responsibility chains) with ease.

Features

  • Flexible task registration system.
  • Context sharing across tasks with built-in state management.
  • Reactive task chaining with hooks.
  • Error handling and progress tracking.
  • Supports React functional components for task definitions.

Installation

npm install react-hook-chain

Basic Usage

  1. Create a Context Interface
// myContext.ts
export interface MyContext {
  id: number;
}
  1. Define Tasks
// task.tsx
import { TaskComponent } from "react-hook-chain";

const ExampleTask: TaskComponent<MyContext> = ({ context, onResolve, onReject }) => {
  const handleContinue = () => {
    onResolve(); // Proceed to the next task
  };

  const handleError = () => {
    onReject("An error occurred!"); // Stop the chain
  };

  return (
    <div>
      <p>Task for ID: {context.id}</p>
      <button onClick={handleContinue}>Continue</button>
      <button onClick={handleError}>Reject</button>
    </div>
  );
};

export default ExampleTask;
  1. Register Tasks Use BizChain to define a sequence of tasks.
// chain.tsx

import Chain from "react-hook-chain";
import ExampleTask from "./ExampleTask";
import { MyContext } from "./myContext";

const mychain = new Chain<MyContext>();

mychain.register("task-1", "Example Task", ExampleTask);

export default mychain;
  1. Use the Chain Leverage useChain to execute tasks reactively in your component.
// app.tsx
import React from "react";
import { useChain } from "react-hook-chain";
import mychain from "./chain";
import { MyContext } from "./myContext";

const App: React.FC = () => {
  const { Chain, run, chainResult } = useChain(chain);

  const handleStart = async () => {
    const { success, errors } = await run({ id: 123 });
    if (success) {
      console.log("All tasks completed successfully!");
    } else {
      console.error("Errors:", errors);
    }
  };

  return (
    <div>
      <button onClick={handleStart}>Start Task Chain</button>
      <Chain />
    </div>
  );
};

export default App;