All Projects → CharlesStover → Fetch Suspense

CharlesStover / Fetch Suspense

Licence: mit
A React hook compatible with React 16.6's Suspense component.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
js
455 projects

Projects that are alternatives of or similar to Fetch Suspense

Use Clippy
React Hook for reading from and writing to the user's clipboard.
Stars: ✭ 139 (-70.98%)
Mutual labels:  npm, travis-ci, travis
Use React Router
React Hook for pub-sub behavior using React Router.
Stars: ✭ 575 (+20.04%)
Mutual labels:  npm, travis-ci, travis
Use Force Update
React Hook to force your functional component to update.
Stars: ✭ 142 (-70.35%)
Mutual labels:  npm, travis-ci, travis
Quicksort Js
An implementation of Quicksort in JavaScript/TypeScript.
Stars: ✭ 60 (-87.47%)
Mutual labels:  npm, travis-ci, travis
Reactn
React, but with built-in global state management.
Stars: ✭ 1,906 (+297.91%)
Mutual labels:  npm, travis-ci, travis
badge-matrix
More advanced badges for projects using Travis or Sauce Labs
Stars: ✭ 77 (-83.92%)
Mutual labels:  travis-ci, travis
fetch
A fetch API polyfill for React Native with text streaming support.
Stars: ✭ 27 (-94.36%)
Mutual labels:  fetch, fetch-api
bestfetch
fetch ⭐️caching ⭐️deduplication
Stars: ✭ 44 (-90.81%)
Mutual labels:  fetch, fetch-api
googletest-ci
Continuous integration (CI) + Google Test (gtest) + CMake example boilerplate demo
Stars: ✭ 14 (-97.08%)
Mutual labels:  travis-ci, travis
fetch-action-creator
Fetches using standardized, four-part asynchronous actions for redux-thunk.
Stars: ✭ 28 (-94.15%)
Mutual labels:  travis-ci, travis
yaspeller-ci
Fast spelling check for Travis CI
Stars: ✭ 60 (-87.47%)
Mutual labels:  travis-ci, travis
react-multi-context
Manage multiple React 16 contexts with a single component.
Stars: ✭ 19 (-96.03%)
Mutual labels:  travis-ci, travis
react-testing-mocha-chai-enzyme
A solid test setup for React components with Mocha, Chai, Sinon, Enzyme in a Webpack/Babel application.
Stars: ✭ 48 (-89.98%)
Mutual labels:  travis-ci, travis
sbt-travisci
An sbt plugin to integrate with Travis CI
Stars: ✭ 44 (-90.81%)
Mutual labels:  travis-ci, travis
legible
the cleanest way to make http requests in js / node
Stars: ✭ 49 (-89.77%)
Mutual labels:  fetch, fetch-api
plugin.video.sendtokodi
📺 plays various stream sites on kodi using youtube-dl
Stars: ✭ 86 (-82.05%)
Mutual labels:  travis-ci, travis
node-fetch-har
Generate HAR entries for requests made with node-fetch
Stars: ✭ 23 (-95.2%)
Mutual labels:  fetch, fetch-api
todo-list
TodoList using Ionic2/3 & Firebase: * PWA * SSO Google plus. * Share list via QRcode. * Upload image from Camera or Storage. * Speech Recognition.
Stars: ✭ 18 (-96.24%)
Mutual labels:  travis-ci, travis
fetch-wrap
extend WHATWG fetch wrapping it with middlewares
Stars: ✭ 21 (-95.62%)
Mutual labels:  fetch, fetch-api
react-innertext
Returns the innerText of a React JSX object.
Stars: ✭ 37 (-92.28%)
Mutual labels:  travis-ci, travis

useFetch Tweet version minified size minzipped size downloads build

useFetch is a React hook that supports the React 16.6 Suspense component implementation.

The design decisions and development process for this package are outlined in the Medium article React Suspense with the Fetch API.

Install

  • npm install fetch-suspense or
  • yarn add fetch-suspense

Examples

Basic Example

import useFetch from 'fetch-suspense';
import React, { Suspense } from 'react';

// This fetching component will be delayed by Suspense until the fetch request
//   resolves. The return value of useFetch will be the response of the server.
const MyFetchingComponent = () => {
  const response = useFetch('/path/to/api', { method: 'POST' });
  return 'The server responded with: ' + response;
};

// The App component wraps the asynchronous fetching component in Suspense.
// The fallback component (loading text) is displayed until the fetch request
//   resolves.
const App = () => {
  return (
    <Suspense fallback="Loading...">
      <MyFetchingComponent />
    </Suspense>
  );
};

Using a Custom Fetch API

If you don't want to rely on the global fetch API, you can create your own useFetch hook by importing the createUseFetch helper function.

import { createUseFetch } from 'fetch-suspense';
import myFetchApi from 'my-fetch-package';
import React, { Suspense } from 'react';

// Create a useFetch hook using one's own Fetch API.
// NOTE: useFetch hereafter refers to this constant, not the default export of
//   the fetch-suspense package.
const useFetch = createUseFetch(myFetchApi);

// This fetching component will be delayed by Suspense until the fetch request
//   resolves. The return value of useFetch will be the response of the server.
const MyFetchingComponent = () => {
  const response = useFetch('/path/to/api', { method: 'POST' });
  return 'The server responded with: ' + response;
};

// The App component wraps the asynchronous fetching component in Suspense.
// The fallback component (loading text) is displayed until the fetch request
//   resolves.
const App = () => {
  return (
    <Suspense fallback="Loading...">
      <MyFetchingComponent />
    </Suspense>
  );
};

Including Fetch Metadata

To include fetch metadata with your response, include an options parameter with metadata: true.

import useFetch from 'fetch-suspense';
import React, { Suspense } from 'react';

// This fetching component will be delayed by Suspense until the fetch request
//   resolves. The return value of useFetch will be the response of the server
//   AS WELL AS metadata for the request.
const MyFetchingComponent = () => {
  const { contentType, response } = useFetch(
    '/path/to/api',
    { method: 'POST' },
    { metadata: true }, // <--
  );
  return `The server responded with ${contentType}: ${response}`;
};

// The App component wraps the asynchronous fetching component in Suspense.
// The fallback component (loading text) is displayed until the fetch request
//   resolves.
const App = () => {
  return (
    <Suspense fallback="Loading...">
      <MyFetchingComponent />
    </Suspense>
  );
};

Options

The supported options for the third, options parameter are:

lifespan?: number

Default: 0

The number of milliseconds to cache the result of the request. Each time the component mounts before this many milliseconds have passed, it will return the response from the last time this same request was made.

If 0, the cache will be last the remainder of the browser session.

metadata?: boolean

Default: false

If true, the useFetch hook will return metadata in addition to the response from the fetch request. Instead of returning just the response, an interface as follows will be returned:

interface UseFetchResponse {
  bodyUsed: boolean;
  contentType: null | string;
  headers: Headers;
  ok: boolean;
  redirected: boolean;
  // The same response from the server that would be returned if metadata were
  //   false. It is an Object is the server responded with JSON, and it is a
  //   string if the server responded with plain text.
  response: Object | string;
  status: number;
  statusText: string;
  url: string;
}

You can access these properties easily through destructuring. See Including Fetch Metadata.

Sponsor 💗

If you are a fan of this project, you may become a sponsor via GitHub's Sponsors Program.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].