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

@gryfnie/react-asyncify

v1.0.1

Published

Say goodbye to messy async code! react-asyncify makes handling asynchronous operations in React a breeze. Whether you’re loading data, dealing with errors, or just want to keep your UI smooth and responsive, this simple, reusable polymorphic component has

Downloads

129

Readme

🔄 @gryfnie/react-asyncify

Say goodbye to messy async code! @gryfnie/react-asyncify makes handling asynchronous operations in React a breeze. Whether you’re loading data, dealing with errors, or just want to keep your UI smooth, this simple, reusable polymorphic component has you covered.

Features

  • 🔄 Polymorphic: Render as any HTML element or custom component
  • 💻 Enhances developer experience with clean, maintainable code
  • 🚀 Simplifies asynchronous operations in React
  • 🔄 Handles loading, success, and error states seamlessly
  • ⚙️ Provides hooks for custom logic with onResolve and onReject
  • 🎨 Fully customizable and easy to integrate
  • ♻️ Reusable across different components and projects
  • 🧩 Provides useAsync hook for custom asynchronous logic
  • 📦 Supports ref for manual revalidation and advanced use cases

Installation

Install the package:

yarn add @gryfnie/react-asyncify

Usage

import React from 'react';
import { Async } from '@gryfnie/react-asyncify';

const fetchData = async () => {
  const response = await fetch('https://api.example.com/data');
  if (!response.ok) throw new Error('Failed to fetch data');
  return await response.json();
};

const MyComponent = () => {
  return (
    <Async
      resolve={fetchData}
      idleRender={() => <>Loading...</>}
      errorRender={(error) => <>{error.message}</>}
      onResolve={(data) => console.log(data)}
      onReject={(error) => console.log(error)}
      as={YourComponent}
    >
      {(props) => <>{props.data} {props.propsFromYourComponent}</>}
    </Async>
  );
};
export default MyComponent;

API

Async Component

The Async component accepts several props to handle different aspects of asynchronous operations in a React application.

Props

  • resolve: () => Promise<T> Required. A function that returns a promise for fetching data. This function is called to retrieve the data when the component mounts or when revalidation occurs.

  • idleRender: () => ReactNode Required. A function that returns a React node to be rendered while data is being loaded. This is typically used to display a loading spinner or message.

  • errorRender: (error: Error) => ReactNode Required. A function that takes an error object as its argument and returns a React node to be rendered in case of an error. This is used to display error messages or fallback UI.

  • children: (data: T) => ReactNode Required. A function that takes the resolved data as its argument and returns a React node to be rendered. This is the main content that will be displayed once the data is successfully fetched.

  • revalidate?: number Optional. A number representing the interval in milliseconds at which the resolve function should be re-triggered to refresh the data. If not provided, the data will only be fetched once when the component mounts.

  • as?: ElementType Optional. A React element type (e.g., 'div', 'section', 'span', React.Fragment, or a custom component) that determines what HTML element or component is rendered. Defaults to React.Fragment.

  • onResolve?: (data: T) => void Optional. A callback function that is called when the data is successfully resolved. This can be used to perform side effects based on the retrieved data.

  • onReject?: (error: Error) => void Optional. A callback function that is called when an error occurs during data fetching. This can be used to log errors or trigger other error-handling mechanisms.

  • ref?: React.Ref<AsyncResolverHandle> Optional. A ref that can be used to manually trigger data revalidation by calling handleRevalidate. This is useful for advanced use cases where you need programmatic control over data fetching.

useAsync Hook

The useAsync hook provides the same asynchronous handling capabilities as the Async component but allows you to implement custom logic within your own components.