All Projects → marcellomontemagno → React Ufo

marcellomontemagno / React Ufo

Licence: mit
🛸 react-ufo - A simple React hook to help you with data fetching 🛸

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Ufo

Frisbee
🐕 Modern fetch-based alternative to axios/superagent/request. Great for React Native.
Stars: ✭ 1,038 (+1121.18%)
Mutual labels:  axios, fetch, fetch-api
React Fetch Hook
React hook for conveniently use Fetch API
Stars: ✭ 285 (+235.29%)
Mutual labels:  hooks, fetch, fetch-api
Redux Requests
Declarative AJAX requests and automatic network state management for single-page applications
Stars: ✭ 330 (+288.24%)
Mutual labels:  axios, fetch
Vue Cli
📃基于 Vue3.0 Composition Api 快速构建实战项目
Stars: ✭ 335 (+294.12%)
Mutual labels:  hooks, axios
Fetch Suspense
A React hook compatible with React 16.6's Suspense component.
Stars: ✭ 479 (+463.53%)
Mutual labels:  fetch, fetch-api
Thwack
A tiny modern data fetching solution
Stars: ✭ 268 (+215.29%)
Mutual labels:  axios, fetch
Swr
React Hooks for data fetching
Stars: ✭ 20,348 (+23838.82%)
Mutual labels:  fetch, hooks
Fetch Examples
A repository of Fetch examples. See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API for the corresponding documentation.
Stars: ✭ 431 (+407.06%)
Mutual labels:  fetch, fetch-api
fetch-wrap
extend WHATWG fetch wrapping it with middlewares
Stars: ✭ 21 (-75.29%)
Mutual labels:  fetch, fetch-api
Axios Hooks
🦆 React hooks for axios
Stars: ✭ 862 (+914.12%)
Mutual labels:  hooks, axios
Node Fetch
A light-weight module that brings the Fetch API to Node.js
Stars: ✭ 7,176 (+8342.35%)
Mutual labels:  fetch, fetch-api
Create Request
Apply interceptors to `fetch` and create a custom request function.
Stars: ✭ 34 (-60%)
Mutual labels:  fetch, fetch-api
react-movies-finder
React Movies finder is a React app to search movies and series using redux, redux-thunk, React Hooks, and Material UI
Stars: ✭ 27 (-68.24%)
Mutual labels:  hooks, axios
re-frame-fetch-fx
js/fetch Effect Handler for re-frame
Stars: ✭ 24 (-71.76%)
Mutual labels:  fetch, fetch-api
fetchx
Beautiful way to fetch data in React
Stars: ✭ 71 (-16.47%)
Mutual labels:  fetch, axios
React Query
⚛️ Hooks for fetching, caching and updating asynchronous data in React
Stars: ✭ 24,427 (+28637.65%)
Mutual labels:  hooks, fetch
openapiclientgen
Generate C# and TypeScript client codes from Open API / Swagger definitions
Stars: ✭ 31 (-63.53%)
Mutual labels:  axios, fetch-api
axios-endpoints
Axios endpoints helps you to create a more concise endpoint mapping with axios.
Stars: ✭ 41 (-51.76%)
Mutual labels:  fetch, fetch-api
React Fetching Library
Simple and powerful API client for react 👍 Use hooks or FACCs to fetch data in easy way. No dependencies! Just react under the hood.
Stars: ✭ 561 (+560%)
Mutual labels:  hooks, fetch
Cross Fetch
Universal WHATWG Fetch API for Node, Browsers and React Native.
Stars: ✭ 1,063 (+1150.59%)
Mutual labels:  fetch, fetch-api

Actions Status

ufo
UFO - Use fetch orderly
A simple React hook to help you with data fetching

Introduction

Fetch API and Axios help you fetching data but when you need to link the status of a request to your React state you are on your own.

Handling the UI state related to a request can be repetitive and error-prone, especially if you have to

  • handle related requests within the same component
  • ignore requests results after your component is unmounted
  • abort requests in certain conditions
  • handle race conditions

Taking advantage of React hooks react-ufo helps you deal with this complexity.

Installation

npm install --save react-ufo

import {useFetcher} from "react-ufo"

How to use

Basic usage

useFetcher handles the state of a request for you and much more.

The minimal usage of useFetcher looks like the following:

const [callback, [loading, error, data]] = useFetcher(fetcher)

A fetcher function is a normal function that fetches some data and returns a promise.

Here an example of fetcher function:

const getTodo = async (id) => {
  const response = await fetch("https://jsonplaceholder.typicode.com/todos/" + id);
  return response.json();
};

When you want your request to start, all you need to do is to invoke callback, after doing so, loading, error, and data will be updated in accordance with the status of your request.

Any argument you pass to callback will be passed to your fetcher.

Note: Do not create a new fetcher function on every render, useFetcher will create a new callback anytime a new fetcher instance is received. In case your fetcher depends on props simply pass them to callback and your fetcher will receive them.

Here a basic example showing how to use useFetcher in an event callback such as onClick Edit 1basicFetchInEventCallbackExample

Fetching on mount/update

By default, before a request is started, useFetcher will return loading=false, error=null, data=null.

Sometimes you might want your initial request state to be different.

One example is if you plan to request your data on the component mount/update, in this case, you might want your initial request state to have loading=true.

useFetcher can receive a second argument indicating the initial state before your request starts.

Here how you override the default loading state to be true

const [callback, [loading, error, data]] = useFetcher(fetcher, {loading:true})

Now if you want your request to start on mount all you need to do is

useEffect(()=>{
  callback()
},[callback])

You don't have to worry about passing callback as a dependency of useEffect, callback will only change if your fetcher changes.

Fetching on mount/update with props

Sometimes a fetcher might need some data in order to retrieve data, for example, the getTodo presented earlier needs an id argument.

Assuming id is a prop of your component all you need to do is

useEffect(()=>{
  callback(id)
},[id,callback])

this ensures that your fetcher will be invoked on mount and anytime id updates, which is usually what you want.

Here a basic example showing how to use useFetcher during mount/update Edit 2basicFetchOnMountAndUpdateExample

Cascading fetches

Sometimes 2 requests depend on each other.

Let's say that you fetched a todo object containing a userId field and you want to use userId to fetch a user object.

Here how you can handle this use case with useFetcher

...

const [fetchTodo, [loadingTodo, todoError, todo]] = useFetcher(todoFetcher, {loading:true})
const [fetchUser, [loadingUser, userError, user]] = useFetcher(userFetcher, {loading:true})

useEffect(()=>{
  fetchTodo(todoId).then((todo)=>{
    fetchUser(todo.userId)
  })
},[todoId, fetchTodo, fetchUser])

...

Here the full example showing this use case Edit 4cascadingFetchesExample

Ignoring a pending request

If your component is unmounted while one of its requests is still pending useFetcher will take care of ignoring its result avoiding an attempt to perform a setState on an unmounted component.

Sometimes you might want to ignore the result of a request for other reasons too.

callback.ignore() can be invoked if you need to ignore the result of a pending request.

If a pending request is marked as ignored loading, error and data will not be updated once the request is completed.

Aborting a pending request

callback.abort() can be invoked anytime you want to abort a pending request.

Unfortunately in order for callback.abort() to work properly there is some little more wiring that you'll need to do.

useFetcher will take care of passing an abort signal to your fetcher as its last argument.

In order for callback.abort() to work you'll need to pass the abort signal to your Fetch API.

Here an example showing how to enable fetch abortion on the getTodo fetcher presented earlier

const getTodo = async (id, signal) => {
  const response = await fetch("https://jsonplaceholder.typicode.com/todos/" + id, {signal});
  return response.json();
};

If your fetcher is not passing the abort signal to fetch API invoking callback.abort() will not abort the request but the request will still be marked as ignored.

If a request is marked as ignored loading, error and data will not be updated once the request is completed.

Here an example showing how to abort a request Edit 3basicAbortFetchExample

Aborting a pending request is quite easy when using Fetch API but it can also be achieved if you are using other libraries such as axios

If you are wondering how to abort a request started by Axios instead of Fetch API you can find an example here Edit abortRequestIfUsingAxiosExample

Keeping state between fetches

By default useFetcher erases the data of a request anytime a new one is started.

Most of the times this is what you want but there are cases where you want to keep the data visible to the user until new data are retrieved.

If you need to keep data between fetches you can simply use useState from React.

Here an example showing how to keep data while multiple request are pending:

const [data, setData] = useState()
const [callback, [loading, error, _data]] = useFetcher(fetcher)

...

const myEventCallback = ()=>{
  callback(1).then((data)=>{
    setData(data)
    callback(2).then((data)=>{
        setData(data)
    })
  })
} 

In the previous example _data is set to null anytime a new request is started while data is only valued when a request is completed.

Debouncing requests

Here an example showing one simple way to debounce requests Edit 5debounceFetchExample

Mutating state

Sometimes you might want to change your request state manually.

One common scenario when this can happen is if your user decides to ignore and remove a request error message displayed on the screen.

useFetcher provides you setLoading, setError, setData and setRequestState for you to handle these use cases.

Here the full signature of useFetcher:

const [callback, [loading, error, data], setRequestState] = useFetcher(fetcher)
const [setLoading, setError, setData] = setRequestState

setLoading, setError, setData and setRequestState should be self explanatory, they work exactly like the setState in const [state, setState] = useState()

Putting all together

Here an example showing how useFetcher can be used to implement a simple CRUD application Edit 6crudExample

useFetcher API

Here the full useFetcher API

const initialRequestState = {loading:false, error:null, data:null} //these are the default values if initialRequestState is not provided
const [callback, requestState, setRequestState] = useFetcher(fetcher, initialRequestState)
const [loading, error, data] = requestState
const [setLoading, setError, setData] = setRequestState

What exactly is useFetcher returning?

useFetcher returns a result object shaped as follow:

{
  callback,
  requestState: {
    loading,
    error,
    data
  },
  setRequestState: {
    setLoading,
    setError,
    setData
  }
}

result, requestState and setRequestState are also iterable, therefore, if you find it convenient for renaming, you can destructure them into an array as follow

const [callback, [loading, error, data], [setLoading, setError, setData]] = result

When destructuring into an array you obviously need to rely on the order we specified for each key, therefore, in case you don't want to extract all the fields from result, you might need to write something like the following:

const [callback, [loading, , data], [, setError]] = result

Because result is an object, accessing its fields by key (e.g const data = result.requestState.data) is going to work as expected too.

Because result is an object, doing object destructuring is going to work as expected too.

Note that even though setRequestState contains setLoading, setError, setData it is a function and can be used to update loading, error and data in a single render.

Note: Even though result, requestState and setRequestState are iterable they are not arrays, therefore something like result[0] or result.requestState[0] is not going to work.

Examples

  1. Basic fetch in event callback Edit 1basicFetchInEventCallbackExample
  2. Basic fetch on mount/update Edit 2basicFetchOnMountAndUpdateExample
  3. Aborting a pending request Edit 3basicAbortFetchExample
  4. Handling requests depending on each others Edit 4cascadingFetchesExample
  5. Debouncing requests Edit 5debounceFetchExample
  6. Simple CRUD application Edit 6crudExample
  7. Aborting a pending request started with axios Edit abortRequestIfUsingAxiosExample

Package versioning

Breaking changes might be made between 0.x.x versions. Starting from version 1.0.0 every breaking change will result in a major version update. The changelog will give you details about every change between versions.

Dependencies

This package has zero dependencies but in order to support fetches abortion you will need AbortController (or a polyfill such as abortcontroller-polyfill) in your environment

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