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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-api-cache

v1.1.0

Published

React component library to help cache API data, resulting in increased performance and decreased mobile data usage.

Downloads

22

Readme

react-api-cache

React component library to help cache API data, resulting in increased performance and decreased mobile data usage. Save trips to the server if unnecessary.

Installation

To install, you can use npm or yarn:

$ npm install react-api-cache
$ yarn add react-api-cache

Examples

Todos within Toggle:

In this example we have a Todos component which fetches todos from an API on mount. If we didn't have a cache, we'd make a request every time the toggle is subsequently opened.

Hook Usage:

import React from 'react'
import { useCache } from 'react-api-cache'
import Toggle from './Toggle'

export default (props) => {
    const { actions } = useCache()
    return (
        <Toggle title="Todos">
            <Todos cacheActions={actions} />
        </Toggle>
    )
}

Component Usage:

import React from 'react'
import Cache from 'react-api-cache'
import Toggle from './Toggle'

export default React.PureComponent {
    render() {
        return (
          <Cache>
            {({ store, actions }) => (
              <Toggle title="Todos">
                <Todos cacheActions={actions} />
              </Toggle>
            )}
          </Cache>
        )
    }
}  

Toggle Component:

import React from 'react'

class Toggle extends React.Component {
  state = {
    open: true,
  }

  toggle = () => this.setState((prevState) => ({
    open: !prevState.open,
  }))

  render() {
    return (
      <div>
        <h2 onClick={this.toggle}>
          <span>{this.props.title}</span>
          <span>
            {`[${this.state.open ? '+' : '-'}]`}
          </span>
        </h2>
        {this.state.open && this.props.children}
      </div>
    )
  }
}

export default Toggle

Todos Component:

import React from 'react'

const url = 'https://jsonplaceholder.typicode.com/todos?_start=0&_limit=100'
const params = {
  _start: 0,
  _limit: 100,
}

class Todos extends React.Component {
  state = {
    loaded: false,
    todos: [],
  }

  componentDidMount() {
    const { hasCache, getCache } = this.props.cacheActions

    if (hasCache(url, params)) {
      this.setTodos(getCache(url, params).data)
    } else {
      fetch(url, params)
        .then((res) => res.json())
        .then(this.setTodos)
    }
  }

  setTodos = (todos) => this.setState({
    loaded: true,
    todos,
  }, () => {
    this.props.cacheActions.setCache(url, params, todos)
  })

  render() {
    if (!this.state.loaded) {
      return <div>Loading...</div>
    }

    return (
      <div>
        {this.state.todos.map((todo) => (
          <div key={todo.id}>
            {todo.title}
          </div>
        ))}
      </div>
    )
  }
}

export default Todos

Seems like a lot, but most of it is boilerplate for components unrelated to usage of this library. This is the most important section:

// ...
  componentDidMount() {
    const { hasCache, getCache } = this.props.cacheActions

    if (hasCache(url, params)) {
      this.setTodos(getCache(url, params).data)
    } else {
      fetch(url, params)
        .then((res) => res.json())
        .then(this.setTodos)
    }
  }

  setTodos = (todos) => this.setState({
    loaded: true,
    todos,
  }, () => {
    this.props.cacheActions.setCache(url, params, todos)
  })
// ...

As seen above, on mount of our component we check if a cache exists for the URL & params combination we're requesting. If the combination exists, we immediately set state from our cache. If now, we pull data from the API. Pretty simple, but fairly powerful!

Public Methods

| Name | Type | Description | | ------------ | --------------------------------- | ------------------------------------------------------------ | | hasCache | (url, params) => boolean | Returns true if a cache exists for the url and params combination passed in. | | getCache | (url, params) => { params, data } | Returns the cache that matches the url and params combination passed in. This method is considered unsafe and should be used following thehasCache method returning true. | | setCache | (url, params, data) => void | Saves cache to store using url, params, and data passed in. This method is considered unsafe and should be used following thehasCache method returning true. | | destroyCache | (?url, ?params) => void | Removes the cache from the store that matches the url and params combination passed in. url and params are both optional and if omitted, the entire cache will be destroyed. This method is considered unsafe and should be used following thehasCache method returning true. |

![](/Users/casey/Downloads/Cache Component (2).png)

License

react-api-cache is released under the MIT license.