Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fsa-redux-thunk

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fsa-redux-thunk

FSA Thunk middleware for Redux.

  • 2.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
55
increased by37.5%
Maintainers
1
Weekly downloads
 
Created
Source

FSA Redux Thunk

Fork of redux-thunk middleware that enforces Flux Standard Actions.

build status npm version npm downloads

Installation

npm install --save redux-thunk

or

yarn add fsa-redux-thunk

Then, to enable Redux Thunk, use applyMiddleware():

import { createStore, applyMiddleware } from 'redux';
import FsaThunk from 'fsa-redux-thunk';
import rootReducer from './reducers/index';

// Note: this API requires redux@>=3.1.0
const store = createStore(
  rootReducer,
  applyMiddleware(FsaThunk)
);

Usage with redux-actions FSA library

Dispatching null initial payload

import { createAction } from 'redux-actions';

const fetchSomeApiSuccess = createAction('FETCH_SOME_API_SUCCESS');
const fetchSomeApiFailure = createAction('FETCH_SOME_API_FAILURE');
const fetchSomeApiRequest = createAction('FETCH_SOME_API_REQUEST', () => dispatch => {
  return fetch
    .get('some/url')
    .catch(err => {
      dispatch(fetchSomeApiFailure(err));
      throw err;
    })
    .then(data => dispatch(fetchSomeApiSuccess(data)));
});

Calling:

store.dispatch(fetchSomeApiRequest(true));

will dispatch an FSA action with this shape first, then will execute the payload creator function defined as the second argument to createAction:

{ type: 'FETCH_SOME_API_REQUEST', payload: null }

Dispatching WITH initial payload

In order to dispatch the initial request action (FETCH_SOME_API_REQUEST in our example), we must provide the value in the meta field, as follows (the third argument to createAction):

import { createAction } from 'redux-actions';

const fetchSomeApiSuccess = createAction('FETCH_SOME_API_SUCCESS');
const fetchSomeApiFailure = createAction('FETCH_SOME_API_FAILURE');
const fetchSomeApiRequest = createAction(
  // action type
  'FETCH_SOME_API_REQUEST',
  // payload creator function
  payload => dispatch => {
    return fetch
      .get('some/url')
      .catch(err => {
        dispatch(fetchSomeApiFailure(err));
        throw err;
      })
      .then(data => dispatch(fetchSomeApiSuccess(data)));
  },
  // meta creator function
  payload => ({ preThunkPayload: payload }),
);

Calling:

store.dispatch(fetchSomeApiRequest('foobar'));

will dispatch an FSA action with this shape first, then will execute the payload creator function defined as the second argument to createAction:

{ type: 'FETCH_SOME_API_REQUEST', payload: 'foobar' }

License

MIT

Keywords

FAQs

Package last updated on 18 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc