Fetches using standardized, four-part asynchronous actions for redux-thunk.
Dispatch a single, asynchronous action that fetches a request, and your redux store will receive corresponding actions when the request (1) dispatches, (2) receives a response, (3) encounters an error, and/or (4) is aborted.
npm install fetch-action-creator --save
oryarn add fetch-action-creator
Your redux store must be using the thunk
middleware.
import fetchActionCreator from 'fetch-action-creator';
const myFetchAction = () =>
fetchActionCreator(
url,
requestInit,
createRequestAction,
createReceiveAction,
createErrorAction,
createAbortAction,
conditional
);
dispatch(myFetchAction()) // fetches url, dispatching asynchronous actions
import fetchActionCreator from 'fetch-action-creator';
const fetchEmployees = () =>
fetchActionCreator(
// URL to request.
'https://my.business.com/employees.json',
// Fetch options.
{
body: 'please',
headers: {
'Content-Type': 'text/plain; charset=utf-8'
},
method: 'GET'
},
// Action for when the request has dispatched.
(abortController) => ({
type: 'REQUEST_EMPLOYEES',
abortController
}),
// Action for when the server has responded.
(employees) => ({
type: 'RECEIVE_EMPLOYEES',
employees
}),
// Action for when an error has occurred.
(err, statusCode) => ({
type: 'EMPLOYEES_ERROR',
error: err,
statusCode
}),
// Action for when the request has been aborted.
() => ({
type: 'ABORT_EMPLOYEES'
}),
// Conditional function for when to disregard this action entirely.
(state) => {
// Don't fetch twice.
if (
state.employees.isFetching ||
Array.isArray(state.employees.list)
) {
return false;
}
return true;
}
);
-
The URL to which you are dispatching a fetch request.
-
The fetch options which you are including in your fetch request or a function that returns said options.
-
An action creator that is called when your fetch request has been dispatched.
-
An AbortController instance that controls that abort signal for the fetch request.
If you desire to abort any of your fetch requests, you should store this instance in your redux state.
If the user's browser does not support aborting requests, the value will be
null
.
-
-
An action creator that is called when your fetch request has received a response.
- A JavaScript object or string with which the server responded to the request.
- The status code with which the server responded to the request.
-
An instance of
Headers
that contains the headers with which the server responded to the request.
-
An action creator that is called when an error occurs.
- A string containing the error message. This may be either a JavaScript error or the response from the server.
-
The status code with which the server responded to the request. If no status code exists (such as during a JavaScript error), the value is
null
.
-
An action creator that is called when the fetch request is aborted.
See also: createRequestAction / Parameters / abortController
-
If present, this function is called prior to the fetch request.
If it returns true, the fetch request will continue. If it returns false, the entire asynchronous action will be ignored.
- Your current redux state.