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-susflow

v1.1.0

Published

[![npm package][npm-img]][npm-url] [![Build Status][build-img]][build-url] [![Downloads][downloads-img]][downloads-url] [![Issues][issues-img]][issues-url]

Downloads

18

Readme

react-susflow

npm package Build Status Downloads Issues

Introduction

react-susflow is a library designed to simplify asynchronous data fetching and integration with React Suspense. By utilizing the sus function, developers can effortlessly make their asynchronous operations work seamlessly with React Suspense, enhancing the loading experience and error handling.

Try it in Codesandbox

Installation

To install react-susflow, run:

npm i react-susflow
yarn add react-susflow
pnpm i react-susflow

Quick Start

The react-susflow library introduces caching capabilities to optimize asynchronous data fetching. Below is a quick guide on utilizing these new features within a React application:

import {Suspense} from "react";
import {sus} from 'react-susflow';

// Define an asynchronous function for data fetching
const fetchData = async (param) => {
  const response = await fetch(`https://api.example.com/data?param=${param}`);
  if (!response.ok) {
    throw new Error('Network response was not ok');
  }
  return response.json();
};

// Wrap the async function with `sus`, specifying cache options
const resource = sus(fetchData, {
  cache: {
    enable: true, // Enable caching
    ttl: 300000 // Cache TTL set to 5 minutes, default to 1 minute
  }
});

const SusComponent = () => {
  const value = resource.read("world!");
  return <>{JSON.stringify(value)}</>;
};

const SusComponent2 = () => {
  const value = resource.read("china!", {useCache: false}); // useCache is set default to true
  return <>{JSON.stringify(value)}</>;
};

// App component with two Suspense wrappers for each resource usage
export default function App() {
  return (
    <>
      <Suspense fallback={<>Loading...</>}>
        <SusComponent/>
      </Suspense>
      <Suspense fallback={<>Loading...</>}>
        <SusComponent2/>
      </Suspense>
    </>
  );
}

This example demonstrates the use of react-susflow with caching enabled, showing how to selectively bypass the cache for certain reads using the useCache: false option.

API Reference

sus(asyncFunction, options?)

Creates a resource from an asynchronous function, optionally configuring caching.

  • Parameters:

    • asyncFunction: An async function that returns a Promise.
    • options (optional): Configuration for caching.
      • cache:
        • enable (optional): Boolean to enable or disable caching.
        • ttl (optional): Time-to-live for cache entries in milliseconds.
  • Returns: An object with a read method for fetching data, either from the cache or by executing the asynchronous function.

read(...params, readOptions?)

Triggers the asynchronous operation, optionally utilizing the cache based on readOptions.

  • Parameters:
    • ...params: Arguments to pass to the asynchronous function.
    • readOptions (optional): Options for reading data.
      • useCache (optional): Boolean to specify whether to use the cache. Setting this to false forces a fresh fetch, bypassing the cache.

By providing a flexible caching mechanism and the ability to control cache usage on a per-read basis, react-susflow enhances data fetching strategies in React applications, offering improved performance and user experience.

Frequently Asked Questions (FAQ)

This section can provide answers to some common questions, such as how to handle errors, how to reload data, etc.

Contribution

Contributions to react-susflow are welcome! Please check out our GitHub repository for more information.

License

react-susflow is released under the MIT license.