react-render-state-hook
TypeScript icon, indicating that this package has built-in type declarations

1.2.4 • Public • Published

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

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Yongwoo Jung
Yongwoo Jung

💻 🤔

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

Readme

Keywords

Package Sidebar

Install

npm i react-render-state-hook

Weekly Downloads

9

Version

1.2.4

License

MIT

Unpacked Size

82.7 kB

Total Files

40

Last publish

Collaborators

  • stegano