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

use-dehydrated-state

v0.1.0

Published

A simple utility Hook for TanStack Query & Remix.

Downloads

26,329

Readme

use-dehydrated-state

use-dehydrated-state is a simple utility Hook for TanStack Query & Remix.

Installation

NPM

npm install use-dehydrated-state
# or
pnpm add use-dehydrated-state
# or
yarn add use-dehydrated-state

Usage

To support caching queries on the server and set up hydration:

  • Create a new QueryClient instance inside of your app, and on an instance ref (or in React state). This ensures that data is not shared between different users and requests, while still only creating the QueryClient once per component lifecycle.
  • Wrap your app component with <QueryClientProvider> and pass it the client instance
  • Wrap your app component with <Hydrate> and pass it the dehydratedState prop from useDehydratedState()
// root.tsx
import { Outlet } from "@remix-run/react";
import {
  Hydrate,
  QueryClient,
  QueryClientProvider,
} from "@tanstack/react-query";

import { useDehydratedState } from "use-dehydrated-state";

export default function MyApp() {
  const [queryClient] = React.useState(() => new QueryClient());

  const dehydratedState = useDehydratedState();

  return (
    <QueryClientProvider client={queryClient}>
      <Hydrate state={dehydratedState}>
        <Outlet />
      </Hydrate>
    </QueryClientProvider>
  );
}

Now you are ready to prefetch some data in your loader.

  • Create a new QueryClient instance for each page request. This ensures that data is not shared between users and requests.
  • Prefetch the data using the clients prefetchQuery method and wait for it to complete
  • Use dehydrate to dehydrate the query cache and pass it to the page via the dehydratedState prop. This is the same prop that useDehydratedState() will pick up to cache in your root.tsx
// pages/invoices.tsx
import { json } from "@remix-run/node";
import { dehydrate, QueryClient, useQuery } from "@tanstack/react-query";

export async function loader() {
  const queryClient = new QueryClient();

  await queryClient.prefetchQuery(["invoices"], getInvoices);

  return json({ dehydratedState: dehydrate(queryClient) });
}

export default function Invoices() {
  const { data } = useQuery({ queryKey: ["invoices"], queryFn: getInvoices });

  // ...
}