react-hook-utils
Install
npm install --save react-hook-utils
Documentation
https://jacob-ebey.github.io/react-hook-utils/
Usage
Global Reducer
Create a global reducer for re-use in components:
// useCount.jsimport globalReducer from "react-hook-utils"; count: 0 ...state count: statecount - 1 ...state count: statecount + 1 ...state count ;
Use the reducer within a component:
import React useCallback from "react"; import useCount from "./useCount"; { // Select only the count property from the state const count decrement increment set = ; // Create a callback to reset the count const reset = ; return <div> <button =>-</button> count <button =>+</button> <br /> <button =>Reset</button> </div> ;}
Use Promise
In the following example, API.getUserAsync returns a Promise. We combine usePromise with useMemo as follows to only make an API call when the userId changes:
import React useMemo from "react";import usePromise from "react-hook-utils"; import API from "./api"; { const user error loading = ; if loading return <div>Loading...</div>; else if error return <div>Something went wrong :</div>; } return <div>Hello username</div>;}