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-async-states-utils

v2.0.0-alpha-7

Published

utilities for react-async-states package

Downloads

21

Readme

Utilities on top of react-async-states

React async states utils

Utilities for react-async-states.

Docs link

State Boundary

What is this ?

Docs link

State boundary is a component that allows performing all render strategies in react:

  1. Render Then Fetch: Render the initial component, then fetch its data
  2. Fetch As You Render: fetch data while rendering and suspend when pending and throw when error.
  3. Fetch Then Render: renders nothing until it fetches data, suppressing the pending state.

StateBoundaryProps

This component accepts the following props:

| Property | Type | Default Value | Description | |----------------|-----------------------------------|-------------------|-----------------------------------------------------------------------------------------------------| | config | MixedConfig<T, E, R, S> | undefined | The same supported configuration as the useAsyncState hook | | dependencies | any[] | [] | The dependencies that will be passed to useAsyncState hook | | strategy | RenderStrategy | RenderThenFetch | The applied strategy | | render | Record<Status, React.ReactNode> | undefined | A record containing the component to render for each status | | children | React.ReactNode | null | children are considered as a fallback whenever the render property doesn't have the actual status |

Where the RenderStrategy enum is defined as follows:


export enum RenderStrategy {
FetchAsYouRender = 0,
FetchThenRender = 1,
RenderThenFetch = 2,
}

useCurrentState()

This hook returns the current state in the boundary

useBoundary(sourceKey?: string)

When Multiple StateBoundary are nested, this hook allows you to take any state up in the tree by its key.

This hook then calls useSource on the result obtained from the config given to that state boundary.

addBooleanStatus

This selector can be passed to useAsyncState, and will add the following properties to the existing state:

isInitial, isPending, isError, isAborted, isSuccess.

These properties are intuitive and well typed along with the equivalent status:

  type User = { username: string, password: string };

  function producer(props: ProducerProps<User, Error, "Timeout">): Promise<User> {
    if (!props.args[0]) throw new Error("username or password is incorrect");
    return Promise.resolve({username: 'admin', password: 'admin'});
  }

  let {state, runc} = useAsyncState({producer, selector: defaultSelector});

  if (state.isPending) {
    let {data} = state; // type of data: null
  }
  if (state.isError) {
    let {data} = state; // type of data: Error
  }
  if (state.isAborted) {
    let {data} = state; // type of data: "Timeout"
  }

  if (state.status === Status.initial) {
    let data = state.data; // ts type of data <- User | undefined
    let {isError, isSuccess} = state;
    if (isSuccess) { // <- type of isSuccess is false
      console.log("impossible")
    }
    if (isError) {  // <- type of isError is false
      console.log('impossible')
    }
  }
  if (state.status === Status.pending) {
    let data = state.data; // ts type of data <- null
  }
  if (state.status === Status.success) {
    let data = state.data; // ts type of data <- User
  }
  if (state.status === Status.error) {
    let data = state.data; // ts type of data <- Error
  }
  if (state.status === Status.aborted) {
    let data = state.data; // ts type of data <- "Timeout"
  }