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-render-state-hook

v1.2.4

Published

React Render State Hook: This hook allows you to declaratively define components that will be rendered based on the data processing state.

Downloads

21

Readme

React Render State Hook

All Contributors NPM License NPM Downloads

React Render State Hook: This hook allows you to declaratively define components that will be rendered based on the data processing state.

Installation

The easiest way to install react-render-state-hook is with npm.

npm install react-render-state-hook

Alternately, download the source.

git clone https://github.com/stegano/react-render-state-hook.git

API Docs

Quick Start

Basic

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

import { useCallback, useEffect } from 'react';
import { useRenderState } from 'react-render-state-hook';

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

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

  return render(
    (data) => <div>Completed({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

Share Rendering 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 { useRenderState } from 'react-render-state-hook';

const shareKey = 'shareKey';

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

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

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

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

  return render(
    (data) => <div>Completed({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

RenderStateProvider

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 {
  RenderStateProvider,
  Store,
  useRenderState,
  IRenderState,
} from 'react-render-state-hook';

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

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

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

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

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

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

DataHandlerExecutorInterceptorList

The dataHandlerExecutorInterceptorList 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 { RenderStateProvider, useRenderState } from 'react-render-state-hook';

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

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

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

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

export const App = ({ children }) => {
  return (
    <RenderStateProvider
      dataHandlerExecutorInterceptorList={[
        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 />
    </RenderStateProvider>
  );
};

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

Examples

Nextjs Todo App with React Render State Hook

  • https://github.com/stegano/nextjs-todo-app-with-react-render-state-hook

Contributors ✨

Thanks goes to these wonderful people (emoji key):

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