Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

use-asset

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-asset

A simple loading strategy for react suspense based on [react-promise-suspense](https://github.com/vigzmv/react-promise-suspense).

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
35K
decreased by-26.82%
Maintainers
1
Weekly downloads
 
Created
Source

A simple loading strategy for react suspense based on react-promise-suspense.

npm install use-asset

Using assets

use-asset allows you to create single assets, which bring their own cache. This is great for preload and cache busting.

import React, { Suspense } from "react"
import { createAsset } from "use-asset"

// First create an asset, it hands you resolve and reject functions.
// The rest of the arguments are user-provided.
// Call resolve whenever you have obtained your result.
const hackerNewsPost = createAsset(async (res, rej, id) => {
  const res = await fetch(`https://hacker-news.firebaseio.com/v0/item/${id}.json`)
  const json = await res.json()
  res(json)
})

// You can preload assets, these will be cached
hackerNewsPost.preload(9000)

function Post({ id }) {
  // Request asset, this component will now suspend
  const { by, title } = hackerNewsPost.read(id)
  return <div>{title} by {by}</div>
}

function App() {
  <Suspense fallback={null}>
    <Post id={9000}>
  </Suspense>
}
Cache busting strategies
// This asset will be removed from the cache in 15 seconds
const hackerNewsPost = createAsset(..., 15000)

// Clear all cached entries
hackerNewsPost.clear()

// Clear a specific entry
hackerNewsPost.clear(9000)
Reading entries outside of suspense
// This will either return the value (without suspense!) or undefined
hackerNewsPost.peek(9000)

Using hooks and global cache

You can opt into use our hook, useAsset, this opens up a global cache, anything you request at any time is written into it.

import { useAsset } from "use-asset"

const hackerNewsPost = async (res, rej, id) => {
  const res = await fetch(`https://hacker-news.firebaseio.com/v0/item/${id}.json`)
  const json = await res.json()
  res(json)
}

function Post({ id }) {
  const { by, title } = useAsset(hackerNewsPost, [id])
  return <div>{title} by {by}</div>
}

function App() {
  <Suspense fallback={null}>
    <Post id={9000}>
  </Suspense>
}
Cache busting and peeking

It has the same api

// Clear all cached entries
useAsset.clear()
// Clear a specific entry
useAsset.clear(9000)
// This will either return the value (without suspense!) or undefined
useAsset.peek(9000)

Keywords

FAQs

Package last updated on 17 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc