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

clean-fetch

v0.1.1

Published

## Simple, Unopinionated, Just Working - React Data Fetching Library

Downloads

48

Readme

Clean Fetch

Simple, Unopinionated, Just Working - React Data Fetching Library

Installation

npm i clean-fetch

Usage

Hook Usage

import useFetch from 'clean-fetch'
  • Signature: useFetch<T>(fetchfn, getInitial).

  • fetchFn: () => Promise<T> | T: a function that returns the data or a promise which resolves to the data.

  • getInitial?: () => T | undefined: an optional function that returns the initial data. If not provided, the initial data is undefined. getInitial() can return undefined, getInitial can be absent, or it can throw an error.

  • Returns {data, error, reload} where:

    • If data and error are both undefined, it means the data is loading or not yet fetched (initial render). They are never both not undefined.
    • reload: a function that takes no argument, reloads the data and returns what the function passed to the hook returns. The reload function reference never changes, you can safely pass it to the independent array of useEffect without causing additional renders. In subsequent renders, reload uses the latest function passed to the hook.
  • useFetch: only fetches data in the first return, only if initial data is not provided. If you want to refetch the data, you need to manually call reload().

const {data, error, reload} = useFetch(() => fetchData(params))
// when params changes, you need to manually call reload()
useEffect(() => void reload(), [params, reload]) // `reload` value never changes

Note

  • useFetch<T>() has a generic type T which is the type of the data returned by the function passed to the hook.
  • When calling reload(), error and data are immediately/synchronously set to undefined (via setState) and the data is refetched.
  • If you want to keep the last data while refetching, for example, to keep the last page of a paginated list until the new page is fetched, you can create a custom hook that retains the last data while fetching the new data.
// only update when value is not undefined
export const useKeep = <T>(value: T): T => {
	const ref = useRef(value)
	if (value !== undefined) ref.current = value
	return value ?? ref.current
}

This useKeep hook is available in misc-hooks package.

  • If you want to delay showing the loading indicator, you should implement that function in caller component.
export const useTimedOut = (timeout: number) => {
	const [timedOut, enable] = useReducer(() => true, false)
	useEffect(() => {
		let cancelled = false
		const timer = setTimeout(() => !cancelled && enable(), timeout)
		return () => {
			cancelled = true
			clearTimeout(timer)
		}
	}, [enable, timeout])
	return timedOut
}

const {data, error, reload} = useFetch(() => fetchData(params))
const timedOut = useTimedOut(500)
return error // has error
	? <ErrorPage/>
	: data // has data
		? <Data data={data}/>
		: timedOut // loading
			? <Loading/>
			: null

This useTimedOut hook is available in misc-hooks package.

  • For now, both data and Error's types are defined. We will improve the type definition in the future.