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

@ryfylke-react/rtk-query-loader

v1.0.7

Published

Lets you create reusable, extendable RTK loaders for React components.

Downloads

100

Readme

RTK Query Loader

npm npm type definitions npm bundle size

RTK Query Loader lets you create query loaders for your React components.

Made for use with RTK Query, but is also query-agnostic.

Install

yarn add @ryfylke-react/rtk-query-loader
# or
npm i @ryfylke-react/rtk-query-loader

Features

  • [x] Flexible: You can extend and inherit existing loaders to create new ones.
  • [x] Transformable: Combine and transform the data from your loaders to your desired format.
  • [x] Query Agnostic: Can be used with RTK Query, Tanstack Query, JS promises, and more...
  • [x] Configurable: Exposes important configuration options, all of which are inheritable.

You can read more about the features @ the docs.


  • aggregateToQuery
    • ✓ It aggregates query status (167 ms)
  • useCreateQuery
    • ✓ It creates a query (107 ms)
    • ✓ The query can throw error (108 ms)
    • ✓ You can refetch the query (645 ms)
    • ✓ Renders loading state until data is available (130 ms)
    • ✓ Will pass arguments properly (129 ms)
  • withLoader
    • ✓ Renders loading state until data is available (132 ms)
    • ✓ onError renders when applicable (130 ms)
    • ✓ onFetching renders when applicable (319 ms)
    • ✓ Internal state won't reset when using whileFetching (272 ms)
    • ✓ Internal state will reset when using onFetching (271 ms)
    • ✓ Can use custom loader component (129 ms)
    • ✓ loaderComponent is backwards compatible (121 ms)
    • ✓ Can defer some queries (231 ms)
    • ✓ Can defer all queries (130 ms)
    • ✓ Loaders with no queries render immediately (4 ms)
    • ✓ Can remount a component that has a failed query (161 ms)
  • createLoader
    • ✓ Normally, deferred queries do not throw (205 ms)
    • ✓ Deferred queries throw error when configured to (209 ms)
    • ✓ Can send static payload to loader (7 ms)
    • ✓ Loader passes props through queriesArg to queries (128 ms)
    • .extend()
      • ✓ Can extend onLoading (5 ms)
      • ✓ Can extend onError (128 ms)
      • ✓ Can extend onFetching (156 ms)
      • ✓ Can extend whileFetching (133 ms)
      • ✓ Can extend queries (122 ms)
      • ✓ Can extend deferred queries (230 ms)
      • ✓ Can extend many times (282 ms)
      • ✓ Can extend with only transform (133 ms)
      • ✓ Can partially extend config (138 ms)

Example

A simple example of a component using rtk-query-loader:

import {
  createLoader,
  withLoader,
} from "@ryfylke-react/rtk-query-loader";

const loader = createLoader({
  useQueries: () => {
    const pokemon = useGetPokemon();
    const currentUser = useGetCurrentUser();

    return {
      queries: {
        pokemon,
        currentUser,
      },
    };
  },
  onLoading: () => <div>Loading pokemon...</div>,
});

const Pokemon = withLoader((props, loader) => {
  const pokemon = loader.queries.pokemon.data;
  const currentUser = loader.queries.currentUser.data;

  return (
    <div>
      <h2>{pokemon.name}</h2>
      <img src={pokemon.image} />
      <a href={`/users/${currentUser.id}/pokemon`}>
        Your pokemon
      </a>
    </div>
  );
}, loader);

What problem does this solve?

Let's say you have a component that depends on data from more than one query.

function Component(props){
  const userQuery = useGetUser(props.id);
  const postsQuery = userGetPostsByUser(userQuery.data?.id, {
    skip: user?.data?.id === undefined,
  });

  if (userQuery.isError || postsQuery.isError){
    // handle error
  }

  /* possible something like */
  // if (userQuery.isLoading){ return (...) }

  return (
    <div>
      {/* or checking if the type is undefined in the jsx */}
      {(userQuery.isLoading || postsQuery.isLoading) && (...)}
      {userQuery.data && postsQuery.data && (...)}
    </div>
  )
}

The end result is possibly lots of bloated code that has to take into consideration that the values could be undefined, optional chaining, etc...

What if we could instead "join" these queries into one, and then just return early if we are in the initial loading stage. That's basically the approach that rtk-query-loader takes. Some pros include:

  • [x] Way less optional chaining in your components
  • [x] Better type certainty
  • [x] Easy to write re-usable loaders that can be abstracted away from the components

Documentation

Quick Guide