Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

React useReducer with async actions

License

NotificationsYou must be signed in to change notification settings

dai-shi/use-reducer-async

Repository files navigation

CInpmsizediscord

React useReducer with async actions

Introduction

React useReducer doesn't support async actions natively.Unlike Redux, there's no middleware interface, but hooks are composable.

This is a tiny library to extend useReducer's dispatchso that dispatching async actions invoke async functions.

Install

npm install use-reducer-async

Usage

import{useReducerAsync}from"use-reducer-async";constinitialState={sleeping:false,};constreducer=(state,action)=>{switch(action.type){case'START_SLEEP':return{ ...state,sleeping:true};case'END_SLEEP':return{ ...state,sleeping:false};default:thrownewError('no such action type');}};constasyncActionHandlers={SLEEP:({ dispatch})=>async(action)=>{dispatch({type:'START_SLEEP'});awaitnewPromise(r=>setTimeout(r,action.ms));dispatch({type:'END_SLEEP'});},};constComponent=()=>{const[state,dispatch]=useReducerAsync(reducer,initialState,asyncActionHandlers);return(<div><span>{state.sleeping ?'Sleeping' :'Idle'}</span><buttontype="button"onClick={()=>dispatch({type:'SLEEP',ms:1000})}>Click</button></div>);};

Notes for abortability

All async action handlers receivesignal in the argument.Referexamples/04_abort/src for the usage.

Note: The implementation depends onAbortController in the DOM spec.If you are using an environment that doesn't have AbortController (for example IE11), you need a polyfill:12

API

useReducerAsync

useReducer with async actions

Parameters

  • reducerR
  • initialStateReducerState<R>
  • asyncActionHandlersAsyncActionHandlers<R, AsyncAction>

Examples

import{useReducerAsync}from'use-reducer-async';constasyncActionHandlers={SLEEP:({ dispatch, getState, signal})=>async(action)=>{dispatch({type:'START_SLEEP'});awaitnewPromise(r=>setTimeout(r,action.ms));dispatch({type:'END_SLEEP'});},FETCH:({ dispatch, getState, signal})=>async(action)=>{dispatch({type:'START_FETCH'});try{constresponse=awaitfetch(action.url);constdata=awaitresponse.json();dispatch({type:'FINISH_FETCH', data});}catch(error){dispatch({type:'ERROR_FETCH', error});}},};const[state,dispatch]=useReducerAsync(reducer,initialState,asyncActionHandlers);

Returns[ReducerState<R>, Dispatch<ExportAction>]

Examples

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

PORT=8080 npm run examples:01_minimal

and openhttp://localhost:8080 in your web browser.

You can also try them in codesandbox.io:01020304

Blogs

Packages

No packages published

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp