{"library":"redux-promise","title":"Redux Promise Middleware","description":"FSA-compliant promise middleware for Redux (v0.6.0). It intercepts actions with a promise as the payload or the action itself as a promise. When the promise resolves, it dispatches a copy of the action with the resolved value and status 'success'; on rejection, it dispatches with the rejected value and status 'error'. The middleware returns a promise to the caller, useful for server-side rendering. It is part of the redux-utilities suite and works seamlessly with redux-actions. Built on the Flux Standard Action (FSA) spec, it requires actions to have a 'type' and optionally 'payload', 'error', and 'meta'. Compatible with all Redux versions. Stable, rarely updated, no known breaking changes since initial release.","language":"javascript","status":"active","last_verified":"Sat Apr 25","install":{"commands":["npm install redux-promise"],"cli":null},"imports":["import promiseMiddleware from 'redux-promise'","import { createAction } from 'redux-actions'; import promiseMiddleware from 'redux-promise'","import { createStore, applyMiddleware } from 'redux'; import promiseMiddleware from 'redux-promise'; const store = createStore(reducer, applyMiddleware(promiseMiddleware))"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { createStore, applyMiddleware } from 'redux';\nimport promiseMiddleware from 'redux-promise';\n\nconst reducer = (state = {}, action) => {\n  switch (action.type) {\n    case 'FETCH_USER_SUCCESS':\n      return { ...state, user: action.payload };\n    default:\n      return state;\n  }\n};\n\nconst store = createStore(\n  reducer,\n  applyMiddleware(promiseMiddleware)\n);\n\n// Action creator returning FSA action with promise payload\nconst fetchUser = (id) => ({\n  type: 'FETCH_USER',\n  payload: fetch(`https://reqres.in/api/users/${id}`).then(res => res.json())\n});\n\nstore.dispatch(fetchUser(1)).then(() => {\n  console.log(store.getState()); // { user: { id: 1, ... } }\n});","lang":"javascript","description":"Demonstrates setup of Redux store with promiseMiddleware, a reducer handling the resolved action, and dispatching an async action with a promise payload.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}