Skip to content

stegano/use-transition-render

Repository files navigation

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):

Yongwoo Jung
Yongwoo Jung

💻 🤔

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published