All Projects → dai-shi → React Hooks Fetch

dai-shi / React Hooks Fetch

Licence: mit
React custom hooks for data fetching with Suspense

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to React Hooks Fetch

Apipeline
Feature-rich and pluggable offline-first API wrapper for all your javascript environements ! Easily wire-up your API and make your app work offline in minutes.
Stars: ✭ 92 (-40.26%)
Mutual labels:  fetch
Vue2 Element
基于vue2 + vue-router2 + element-ui + vuex2 + fetch + webpack2 企业级后台管理系统最佳实践
Stars: ✭ 112 (-27.27%)
Mutual labels:  fetch
Fetch
Fetch Standard
Stars: ✭ 1,794 (+1064.94%)
Mutual labels:  fetch
Redux Cnode
react+react-router+redux+es6+antd-mobile+webpack版本的Cnode
Stars: ✭ 96 (-37.66%)
Mutual labels:  fetch
Jmusic
重构一款音乐app
Stars: ✭ 108 (-29.87%)
Mutual labels:  fetch
Fetch Plus
🐕 Fetch+ is a convenient Fetch API replacement with first-class middleware support.
Stars: ✭ 116 (-24.68%)
Mutual labels:  fetch
Fetcher Ts
Type-safe wrapper around Fetch API
Stars: ✭ 87 (-43.51%)
Mutual labels:  fetch
Holen
Declarative fetch for React
Stars: ✭ 152 (-1.3%)
Mutual labels:  fetch
Electron Vue Cloud Music
🚀Electron + Vue 仿网易云音乐windows客户端
Stars: ✭ 1,894 (+1129.87%)
Mutual labels:  fetch
Tipple
A lightweight dependency-free library for fetching data over REST with React.
Stars: ✭ 133 (-13.64%)
Mutual labels:  fetch
Rsfetch
Fast (~1ms execution time) and somewhat(?) minimal fetch program written in Rust.
Stars: ✭ 103 (-33.12%)
Mutual labels:  fetch
Hyperapp Fx
Effects for use with Hyperapp
Stars: ✭ 105 (-31.82%)
Mutual labels:  fetch
Vue Spa Project
vue.js + vuex + vue-router + fetch + element-ui + es6 + webpack + mock 纯前端SPA项目开发实践
Stars: ✭ 118 (-23.38%)
Mutual labels:  fetch
Php Fetch
A simple, type-safe, zero dependency port of the javascript fetch WebApi for PHP
Stars: ✭ 95 (-38.31%)
Mutual labels:  fetch
Use Http
🐶 React hook for making isomorphic http requests
Stars: ✭ 2,066 (+1241.56%)
Mutual labels:  fetch
Mfetch
mfetch will provide you with a strong ability to request resource management
Stars: ✭ 90 (-41.56%)
Mutual labels:  fetch
Nion
🌵 Declarative API Data Management Library built on top of redux 🌵
Stars: ✭ 116 (-24.68%)
Mutual labels:  fetch
React Fetch Component
React component to declaratively fetch data
Stars: ✭ 153 (-0.65%)
Mutual labels:  fetch
React Infinite Scroll Hook
A simple hook to create infinite scroll list components
Stars: ✭ 151 (-1.95%)
Mutual labels:  fetch
Nuxt Dev To Clone
Build DEV.TO clone with Nuxt.js and new `fetch` hook
Stars: ✭ 118 (-23.38%)
Mutual labels:  fetch

react-hooks-fetch

Build Status npm version bundle size

React custom hooks for data fetching with Suspense

Important notes

If you are looking for useEffect-based React hooks for Fetch API, please visit https://github.com/dai-shi/react-hooks-async.

This is a library for async functions with Suspense

Introduction

Suspense for data fetching is a new feature coming in React. This library provides useFetch hook for Render-as-You-Fetch. It discourages Fetch-on-Render pattern.

Install

npm install react-hooks-fetch

Usage

import React, { Suspense, useTransition } from 'react';
import ReactDOM from 'react-dom';

import { ErrorBoundary, createFetchStore, useFetch } from 'react-hooks-fetch';

const DisplayData = ({ result, refetch }) => {
  const [startTransition, isPending] = useTransition({
    timeoutMs: 1000,
  });
  const onClick = () => {
    startTransition(() => {
      refetch('2');
    });
  };
  return (
    <div>
      <div>First Name: {result.data.first_name}</div>
      <button type="button" onClick={onClick}>Refetch user 2</button>
      {isPending && 'Pending...'}
    </div>
  );
};

const fetchFunc = async (userId) => (await fetch(`https://reqres.in/api/users/${userId}?delay=3`)).json();
const store = createFetchStore(fetchFunc);
store.prefetch('1');

const Main = () => {
  const [result, refetch] = useFetch(store, '1');
  return <DisplayData result={result} refetch={refetch} />;
};

const App = () => (
  <ErrorBoundary fallback={error => <h1>{error.message}</h1>}>
    <Suspense fallback={<span>Loading...</span>}>
      <Main />
    </Suspense>
  </ErrorBoundary>
);

ReactDOM.createRoot(document.getElementById('app')).render(<App />);

API

createFetchStore

create fetch store

Parameters

  • fetchFunc FetchFunc<Result, Input>
  • preloaded Array<{input: Input, result: Result}>?

Examples

import { createFetchStore } from 'react-hooks-fetch';

const fetchFunc = async (userId) => (await fetch(`https://reqres.in/api/users/${userId}?delay=3`)).json();
const store = createFetchStore(fetchFunc);
store.prefetch('1');

ErrorBoundary

Extends Component

ErrorBoundary with retry support

Examples

import { ErrorBoundary } from 'react-hooks-fetch';

const App = () => (
  <ErrorBoundary
    fallback={({ error, retry }) => (
      <div>
        <span>{error.message}</span>
        <button type="button" onClick={retry}>Retry</button>
      </div>
    )}
  >
    ...
  </ErrorBoundary>
};

useFetch

useFetch hook

Parameters

  • store FetchStore<Result, Input>
  • initialInput Input?

Examples

import { useFetch } from 'react-hooks-fetch';

const [result, refetch] = useFetch(store, initialInput);

Examples

The examples folder contains working examples. You can run one of them with

PORT=8080 npm run examples:01_minimal

and open http://localhost:8080 in your web browser.

You can also try them in codesandbox.io: 01 02 03

Blogs

See History for previous implementations.

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].