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

use-transition-render

v1.0.2

Published

Transition Render Hook(useTransitionRender): This hook allows you to declaratively define components that will be rendered based on the data processing state.

Downloads

10

Readme

Transition Render Hook(useTransitionRender)

All Contributors NPM License NPM Downloads

Transition Render Hook(useTransitionRender): This hook allows you to declaratively define components that will be rendered based on the data processing state.

Installation

The easiest way to install use-transition-render is with npm.

npm install use-transition-render

Alternately, download the source.

git clone https://github.com/stegano/use-transition-render.git

Quick Start

Basic

The useTransitionRender hook enables a declarative approach to display components based on data processing status.

import { useCallback, useEffect } from 'react';
import { useTransitionRender } from 'use-transition-render';

export const App = () => {
  const [render, handleData] = useTransitionRender<string, Error>();

  useEffect(() => {
    handleData(async () => {
      return 'Hello World';
    });
  }, [handleData]);

  return render(
    (data) => <div>Success({data})</div>,
    <p>Idle</p>,
    <p>Loading..</p>,
    (error) => <p>Error, Oops something went wrong.. :(, ({error.message})</p>
  );
};

Demo: https://stackblitz.com/edit/stackblitz-starters-uv8yjs

Advanced features

Sharing Render Data

Without state management libraries like Redux, it is possible to share data and rendering state among multiple containers(components).

import { useCallback, useEffect } from 'react';
import { useTransitionRender } from 'use-transition-render';

const shareKey = 'shareKey';

export const ComponentA = () => {
  const [render, handleData] = useTransitionRender<string, Error>(
    undefined,
    shareKey
  );

  useEffect(() => {
    handleData(async () => {
      return 'Hello World';
    });
  }, [handleData]);

  return render(
    (data) => <div>Success({data})</div>,
    <p>Idle</p>,
    <p>Loading..</p>,
    (error) => <p>Error, Oops something went wrong.. :(, ({error.message})</p>
  );
};

export const ComponentB = () => {
  const [render, handleData] = useTransitionRender<string, Error>(
    undefined,
    shareKey
  );

  return render(
    (data) => <div>Success({data})</div>,
    <p>Idle</p>,
    <p>Loading..</p>,
    (error) => <p>Error, Oops something went wrong.. :(, ({error.message})</p>
  );
};

export const App = () => {
  return (
    <>
      <ComponentA />
      <ComponentB />
    </>
  );
};

Demo: https://stackblitz.com/edit/stackblitz-starters-gb4yt6

TransitionRenderProvider

Store

Through the store option, you can create and pass an internally used store for state management. Additionally, you can enable debugging settings for the store object to check logs in the browser console or register necessary middlewares.

import { useEffect } from 'react';
import {
  TransitionRenderProvider,
  Store,
  useTransitionRender,
  ITransitionRender,
} from 'use-transition-render';

const customStroe = Store.createStore<ITransitionRender.DataHandlingState<any, any>>(
  {
    debug: true,
    middlewareList: [
      (id, next, store) => {
        next.data += ` World | ${id} | ${JSON.stringify(store)}`;
        return next;
      },
    ],
  }
);

const Component = () => {
  const [render, handleData] = useTransitionRender<string>();

  useEffect(() => {
    handleData(() => 'Hello');
  }, [handleData]);

  return render((data) => <p>{data}</p>);
};

export const App = () => {
  return (
    <TransitionRenderProvider store={customStroe}>
      <Component />
    </TransitionRenderProvider>
  );
};

Demo: https://stackblitz.com/edit/stackblitz-starters-vc1jnu

InterceptorList

The interceptorList can intercept the execution of dataHandlerExecutor enabling you to transform it. This can be beneficial for tasks such as adding logs to track data processing or injecting dummy data for use in Storybook and testing environments.

import { useCallback, useEffect } from 'react';
import { TransitionRenderProvider, useTransitionRender } from 'use-transition-render';

// 'greeting' is the executorId. This value serves as an identifier in `dataHandlerExecutorInterceptor` to distinguish tasks.
const greetingId = 'greeting';

const Component = () => {
  const [render, handleData] = useTransitionRender<string>();

  useEffect(() => {
    handleData(() => 'Hi', greetingId);
  }, [handleData]);

  return render((greeting) => <p>{greeting}</p>);
};

export const App = ({ children }) => {
  return (
    <TransitionRenderProvider
      interceptorList={[
        async (_previousInterceptorResult, dataHandlerExecutor, executorId) => {
          if (executorId === greetingId) {
            // The `dataHandlerExecutor` with an executorId value of 'greeting' is not actually executed instead, this provider returns the value 'Hello'.
            return 'Hello';
          }
          return await dataHandlerExecutor();
        },
      ]}
    >
      <Component />
    </TransitionRenderProvider>
  );
};

Demo: https://stackblitz.com/edit/stackblitz-starters-hfd32h

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!