Skip to content

Instantly share code, notes, and snippets.

View simonrelet's full-sized avatar
🏠
Working from home

Simon Relet simonrelet

🏠
Working from home
  • Paris
View GitHub Profile
@simonrelet
simonrelet / asynchronous.js
Last active March 18, 2023 20:13
React hooks for asynchronous calls
import React from 'react'
/**
* @typedef {object} State The state of asynchronous hooks.
* @property {object | null} error The error.
* @property {boolean} pending Whether the call is pending.
* @property {any | null} result The result of the asynchronous call.
*/
/** @type {State} */
@simonrelet
simonrelet / useFetch.js
Last active June 4, 2021 22:27
React Request's Fetch component transposed in hooks
import { fetchDedupe, getRequestKey, isRequestInFlight } from 'fetch-dedupe'
import { useEffect, useRef, useState } from 'react'
// This in the Fetch component transposed in hooks.
// https://github.com/jamesplease/react-request/issues/199
// https://github.com/jamesplease/react-request/blob/master/src/fetch.js
// This object is our cache.
// The keys of the object are requestKeys.
// The value of each key is a Response instance.