Giter Site home page Giter Site logo

andereb081 / react-api-cache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from caseymorrisus/react-api-cache

0.0 0.0 0.0 191 KB

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

JavaScript 97.16% CSS 1.48% HTML 1.37%

react-api-cache's Introduction

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.

react-api-cache flowchart

License

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

react-api-cache's People

Contributors

caseymorrisus avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.