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

axios-cache-hooks

v1.0.3

Published

The faster, simplest and most efficient way to use Axios inside React applications

Downloads

1,062

Readme

Issues Stars License Codecov FOSSA Status Join the chat at https://gitter.im/tinylibs-js-org/community Speed Blazing

Latest Version Downloads JsDelivr Bundlephobia Packagephobia

Table of Contents

Installing

npm   install axios axios-cache-interceptor axios-cache-hooks
yarn  add     axios axios-cache-interceptor axios-cache-hooks
import { createAxiosHooks } from 'axios-cache-hooks';
const { createAxiosHooks } = require('axios-cache-hooks');
const { createAxiosHooks } = window.axiosCacheHooks;
<script
  crossorigin
  src="https://cdn.jsdelivr.net/npm/axios-cache-hooks@latest/dist/index.umd.js"
></script>

Url Import

import { createAxiosHooks } from 'https://cdn.skypack.dev/axios-cache-hooks@latest';

Getting Started

Axios Cache Hooks is a super effective and performant way to use Axios calls inside React applications. It is very performant because it deeply uses (and only works with) Axios (6.7Kb) and Axios Cache Interceptor (4.2Kb) under the hood.

// http.ts
import Axios from 'axios';
import { setupCache } from 'axios-cache-interceptor';
import { createAxiosHooks } from 'axios-cache-hooks';

export const axios = setupCache(Axios);
export const { useQuery, useMutation } = createAxiosHooks();

/** Returns an user by his name */
export function getUser(name: string, config?: AxiosRequestConfig): Promise<User> {
  return axios.get<User>(`users/find-by-name/${name}`, config);
}
// component.tsx
import { useQuery, getUser } from './http';

export const UserAge = ({ username }) => {
  // This will share cache between ALL components that uses the same query and parameters.
  const [user, { loading, error }] = useQuery(getUser, username);

  return (
    <Layout>
      <p>{loading ? 'loading...' : user.age}</p>
    </Layout>
  );
};

Documentation

This package is just a "bridge" between Axios with Cache and React. Please read the Axios Cache Interceptor documentation for any cache issue.

This package is so small that every documentation is available in the form of TSDoc. You can start by importing createAxiosHooks and using the returned hooks.

How it works

Basically, this package calls the provided http function on every draw, this is fine because you can only use this hook with axios-cache-interceptor which caches very effectively axios request, only allowing needed axios request to go through network.

By extracting you request functions into dedicated functions, like the getUser above, you'll enable caching requests at a component level, even for your micro-frontend setup.

This works flawlessly because Axios Cache Interceptor has a concept of Request IDs that defines which requests are the same or not.

If you still have any questions, please feel free to create an issue and i'll be happy to help (and even improve this readme).

Compatibility

This package is dependent of AxiosCacheInterceptor@>=0.8 and Axios@>=0.28 because of this PR.

License

Licensed under the MIT. See LICENSE for more informations.

FOSSA Status