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-query-async

v1.0.10

Published

A HOC utility tool built for [react-query](https://www.npmjs.com/package/react-query) however also supports [swr](https://www.npmjs.com/package/swr) and [@apollo/graphql](https://www.npmjs.com/package/@apollo/graphql) as well as any custom data fetch hook

Downloads

92

Readme

React-Query-Async

A HOC utility tool built for react-query however also supports swr and @apollo/graphql as well as any custom data fetch hook.

Features

  • Async (HOC)
  • useAsync (Hook)
  • AsyncProvider (Config exposed to all instances of Async and useAsync)

Quick Setup

import { RQMergeStatesFn, SWRMergeStatesFn, GQLMergeStatesFn } from "react-query-async" 

Out of the box configured with RQMergeStatesFn which supports react-query however SWRMergeStatesFn and GQLMergeStatesFn provided for convenience. The latter two have not been fully tested yet.

Usage with React-Query

Without Async

import { useQuery, useMutation } from "react-query"

const Loading = () => <div>Loading</div>;
const Error = () => <div>Error</div>;
const NoData = () => <div>No Data</div>;

const App = () => {
  const todosQuery = useQuery('todos', () => API.getTodos());
  const { mutate, ...createTodoMutation } = useMutation(API.createTodo);
  
  if (todosQuery.isLoading || createTodoMutation.isLoading) return <Loading />
  
  if (todosQuery.isError || createTodoMutation.isError) return <Error />
  
  if (!!todosQuery.data) return <NoData />
  
  const { data: todos } = todosQuery;
  
  return (
    <>
      {todos.map(todo => <div>{todo.name}</div>)}
      
      <button 
        onClick={() => {
          mutate({ name: "I'm a new Todo!"})
        }}
      >
        Create New Todo
      </button>
    </>    
  )
};

With Async

Instead of having to constantly define stateful views, write it once and feed it into the Async HOC. Wrap your code with the Async HOC and it will do the rest.

import { Async } from "react-query-async"
import { useQuery, useMutation } from "react-query"

const Loading = () => <div>Loading</div>;
const Error = () => <div>Error</div>;
const NoData = () => <div>No Data</div>;

const App = () => {
  const todosQuery = useQuery('todos', () => API.getTodos());
  const { mutate, ...createTodoMutation} = useMutation(API.createTodo);

  return (
    <Async 
      queries={{ todosQuery }} 
      mutations={{ createTodoMutation }} 
      components={{ Loading, Error, NoData }}
    >
      {({
        queries: {
          todosQuery: { data: todos }
        }
      }) => (
        <>
          {todos.map(todo => <div>{todo.name}</div>)}
          <button 
            onClick={() => {
              mutate({ name: "I'm a new Todo!"})
            }}
          >
            Create New Todo
          </button>
        </>
      )}
    </Async>
  );
};

With AsyncProvider

Don't want to supplement the components prop everytime? Set up the config to whatever you want with.
import { Async } from "react-query-async"

const Loading = () => <div>Loading</div>;
const Fetching = () => <div>Fetching</div>;
const Error = () => <div>Error</div>;
const NoData = () => <div>No Data</div>;

const config = {
  showFetching: true,
  components: { Loading, Error, NoData, Fetching }  
}

ReactDOM.render( 
  <AsyncProvider config={config}> // Config will be passed to all instances of Async
    <App />
  </AsyncProvider>,
document.getElementById('root')
);

API

Async

<Async 
  queries={{ query1, query2 }} 
  mutations={{ mutation1 }} 
  showFetching 
  components={{ 
    Loading: () => "Custom Loading", // Supports React Components or 
    Fetching: "Fetching!" // even just a string!
  }}
  ErrorBoundary={MyCustomErrorBoundary} // Wrap instance with your own error boundary
>
  {({
   queries,
   mutations,
   queryState,
   mutationState.
  }) => "Data here!"}
</Async>
Parameters
  • queries?: any

    • Any async operation that fetches data
    • Takes in a key value hash i.e queries={{ todosQuery, postsQuery }}
  • mutations?: any

    • Any async operation that updates data
    • Takes in a key value hash i.e mutations={{ addTodosMutation, deleteTodoMutation }}
  • isLoading?: boolean | (() => boolean)

  • isFetching?: boolean | (() => boolean)

  • hasError?: boolean | (() => boolean)

  • hasData?: boolean | (() => boolean)

  • showFetching?: boolean

    • By default false so Fetch State will not be displayed
  • components?: DefaultComponents

    • DefaultComponents
      • Loading: ({ queryState, mutationState, queries, mutations }) => any | React.ReactNode | string
      • Fetching: ({ queryState, mutationState, queries, mutations }) => any | React.ReactNode | string
      • Error: ({ queryState, mutationState, queries, mutations }) => any | React.ReactNode | string
      • NoData: ({ queryState, mutationState, queries, mutations }) => any | React.ReactNode | string
    • Feel free to configure state to whatever you want. All the information you need is passed back so it's as flexible as possible
  • mergeQueryStatesFn?: (operations: any) => OperationState

    • Out of the box it is defaulted to support react-query however see Examples to configure to swr, @apollo/graphql or any custom hook!

    • Any custom hook supported as long as operations is reduced to:

      • isLoading
      • hasError
      • hasData
      • isFetching - Optional
    • React Query Example - Default used

      const RQMergeStatesFn = (operations: any) => {
        return Object.values(operations)
          .filter((el: any) => el.status !== 'idle')
          .reduce(
            (acc: any, el: any) => ({
              ...acc,
              isLoading: !!(acc.isLoading || el.isLoading),
              isPaused: !!(acc.isPaused || el.isPaused),
              isFetching: !!(acc.isFetching || el.isFetching),
              hasError: !!(acc.hasError || el.isError),
              isSuccess: !!(acc.isSuccess === undefined ? el.isSuccess : acc.isSuccess && el.isSuccess),
              hasData: !!(acc.hasData === undefined
                ? getHasData(el.data)
                : acc.hasData && getHasData(el.data)),
              status: getStatus({
                isLoading: acc.isLoading || el.isLoading,
                hasError: acc.hasError || el.isError,
                isSuccess: acc.isSuccess === undefined ? el.isSuccess : acc.isSuccess && el.isSuccess
              })
            }),
            {}
          ) as OperationState;
      };
  • mergeMutationStatesFn?: (operations: any) => OperationState

    • Out of the box it is defaulted to support react-query however see Examples to configure to swr, @apollo/graphql or any custom hook!
    • Don't like that mutations also trigger hard loading? Redefine the mergeMutationStatesFn to to exclude hard loading states!
    • Any custom hook supported as long as operations is reduced to:
      • isLoading
      • hasError
      • hasData
      • isFetching - Optional
  • ErrorBoundary?: any

    • In case you want each instance of the Async HOC to have an Error Boundary as a fall back.
  • errorBoundaryProps?: any

    • Props to be passed to ErrorBoundary
Returns
  • queryState: OperationState
    • The state of all queries
    • OperationState
      • isLoading if at least 1 query is loading
      • isFetching if at least 1 query is fetching
      • hasError if at least 1 query has error
      • hasData if at least 1 query has no Data
  • mutationState: OperationState
    • The state of all mutations
    • OperationState
      • isLoading if at least 1 query is loading
      • isFetching if at least 1 query is fetching
      • hasError if at least 1 query has error
      • hasData if at least 1 query has no Data
  • queries: any
    • Same as Params
  • mutations: any
    • Same as Params

useAsync

const [queryState, mutationState] = useAsync({ queries: { query1, query2 }, mutations: { mutation1 }})
Parameters
  • queries?: any
    • Same as Async
  • mutations?: any
    • Same as Async
  • mergeQueryStatesFn?: (operations: any) => OperationState
    • Same as Async
  • mergeMutationStatesFn?: (operations: any) => OperationState
    • Same as Async
Returns
  • queryState: OperationState
    • The state of all queries
    • OperationState
      • isLoading if at least 1 query is loading
      • isFetching if at least 1 query is fetching
      • hasError if at least 1 query has error
      • NoData if at least 1 query has no Data
  • mutationState: OperationState
    • The state of all mutations
    • OperationState
      • isLoading if at least 1 query is loading
      • isFetching if at least 1 query is fetching
      • hasError if at least 1 query has error
      • NoData if at least 1 query has no Data

AsyncProvider

<AsyncProvider 
  config={
    showFetching: false,
    components={{
      Loading: "Custom Loading",
      Fetching: "Custom Fetching",
      Error: () => "Custom Error",
      NoData: () => "Custom No Data"
    }}
    mergeQueryStatesFn={customMergeFn}
    mergeMutationStatesFn={customMergeFn}
    ErrorBoundary={MyCustomErrorBoundary}
    errorBoundaryProps={ fallback: "I'm an error fallback"}
  }
>
  <App />
</AsyncProvider>
  • config
    • Same as Async

Examples