- Notifications
You must be signed in to change notification settings - Fork 10
thunk
Thunk is a repatch middleware to handle async actions. It is very similar to redux-thunk. Thunk middleware allows you to create reducers that returns a function (delegate). The delegate will be fired at dispatching.
Thunk: State -> Delegate
importStore,{thunk}from'repatch'conststore=newStore({items: []}).addMiddleware(thunk)store.dispatch(_=>async(dispatch,getState)=>{constitems=awaitfetch('/items')dispatch(state=>({ ...state, items }))})importStore,{thunk}from'repatch'importapifrom'./api'conststore=newStore({items: []}).addMiddleware(thunk.withExtraArgument(api))store.dispatch(_=>async(dispatch,getState,api)=>{constitems=awaitapi.get('/items')dispatch(state=>({ ...state, items }))})dispatch (Store.dispatch): The
dispatchmethod of theStoreinstance. With this you can dispatch any reducer to the store.getState (Store.getState): The
getStatemethod of theStoreinstance. With this you can get the current state of the store.extraArgument (any): An extra argument that you can provide at adding the thunk middleware. The
extraArgumentcould be anything what you want. It is useful to keep the delegates side-effect-less.
(any): Delegate can return anything. If your delegate returns a Promise, you can chain your async actions to each other.