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-asynchronous-manager

v1.0.3

Published

A React package for managing asynchronous operations and state.

Downloads

11,068

Readme

AsyncManager

AsyncManager is a powerful React library for managing asynchronous operations and state with ease. It simplifies handling loading, error, and success states, making it easier to manage complex async workflows in your React applications.

Table of Contents

Features

  • Simplified Async Management: Manage loading, error, and success states with ease.
  • Customizable Hooks: Create custom hooks for reusable async operations.
  • Modular and Performant: Designed for large applications with performance optimizations.
  • Easy Integration: Simple to integrate with existing React applications.
  • Extensive Documentation: Comprehensive guides and examples to get you started.

Installation

Install AsyncManager using npm or yarn:

npm install react-asynchronous-manager

or

yarn add react-asynchronous-manager

Usage

Basic Example

Here’s a basic example of how to use AsyncManager in a React component:

import React from 'react';
import { useAsyncManager } from 'react-asynchronous-manager';

const fetchData = async () => {
    const response = await fetch('https://api.example.com/data');
    return response.json();
};

const MyComponent: React.FC = () => {
    const AsyncManager = useAsyncManager();
    const [state, fetchData] = AsyncManager.useAsyncState('data', { loading: false, data: null, error: null });

    React.useEffect(() => {
        fetchData(fetchData);
    }, [fetchData]);

    if (state.loading) return <p>Loading...</p>;
    if (state.error) return <p>Error: {state.error.message}</p>;

    return (
        <div>
            <h1>Data:</h1>
            <pre>{JSON.stringify(state.data, null, 2)}</pre>
        </div>
    );
};

export default MyComponent;

Advanced Usage

Combining Multiple Async States

AsyncManager allows you to manage multiple async states within a single component:

import React from 'react';
import { useAsyncManager } from 'react-asynchronous-manager';

const fetchUserData = async () => {
    const response = await fetch('https://api.example.com/user');
    return response.json();
};

const fetchPostsData = async () => {
    const response = await fetch('https://api.example.com/posts');
    return response.json();
};

const CombinedComponent: React.FC = () => {
    const AsyncManager = useAsyncManager();
    const [userState, fetchUser] = AsyncManager.useAsyncState('user', { loading: false, data: null, error: null });
    const [postsState, fetchPosts] = AsyncManager.useAsyncState('posts', { loading: false, data: null, error: null });

    React.useEffect(() => {
        fetchUser(fetchUserData);
        fetchPosts(fetchPostsData);
    }, [fetchUser, fetchPosts]);

    if (userState.loading || postsState.loading) return <p>Loading...</p>;
    if (userState.error || postsState.error) return <p>Error: {userState.error?.message || postsState.error?.message}</p>;

    return (
        <div>
            <h1>User Data:</h1>
            <pre>{JSON.stringify(userState.data, null, 2)}</pre>
            <h1>Posts Data:</h1>
            <pre>{JSON.stringify(postsState.data, null, 2)}</pre>
        </div>
    );
};

export default CombinedComponent;

Custom Hooks

You can create custom hooks that leverage AsyncManager for reusable logic:

import React from 'react';
import { useAsyncManager } from 'react-asynchronous-manager';

const useCustomData = (url: string) => {
    const AsyncManager = useAsyncManager();
    const [state, fetchData] = AsyncManager.useAsyncState('customData', { loading: false, data: null, error: null });

    React.useEffect(() => {
        fetchData(() => fetch(url).then(res => res.json()));
    }, [url, fetchData]);

    return state;
};

const MyComponent: React.FC = () => {
    const state = useCustomData('https://api.example.com/data');

    if (state.loading) return <p>Loading...</p>;
    if (state.error) return <p>Error: {state.error.message}</p>;

    return (
        <div>
            <h1>Data:</h1>
            <pre>{JSON.stringify(state.data, null, 2)}</pre>
        </div>
    );
};

export default MyComponent;

Performance Optimization

Tips for Large Applications

  1. Lazy Loading: Load async operations only when necessary to reduce initial load time.
  2. Memoization: Use memoized callbacks for async actions to avoid unnecessary re-renders.
  3. Error Boundaries: Implement error boundaries to catch and handle errors gracefully within your component tree.

Showcase Application

We’ve created a showcase application that demonstrates the capabilities of AsyncManager. You can find the source code and a live demo in the showcase-app directory.

Testing

We’ve ensured comprehensive test coverage for AsyncManager:

  1. Unit Tests: Written using Jest to test individual components and hooks.
  2. End-to-End Tests: Written using Cypress to simulate real-world usage scenarios.

To run the tests:

npm test

CI/CD Pipeline

AsyncManager includes a CI/CD pipeline powered by GitHub Actions, which handles:

  1. Code linting and formatting
  2. Running unit and end-to-end tests
  3. Building and publishing the package

The pipeline is configured in the .github/workflows/node.js.yml file.

##Contributing We welcome contributions! Please read our CONTRIBUTING.md for guidelines on how to contribute to this project.

##License AsyncManager is licensed under the MIT License.