1.0.5 • Published 7 years ago

redux-async-thunk v1.0.5

Weekly downloads
14
License
ISC
Repository
github
Last release
7 years ago

redux-async-thunk

Expension for thunk middleware for redux to enable async/await or generator/yield flow. No need to install thunk middleware, it is supported as well.

installation

npm install redux-async-thunk --save

usage

Apply redux-async-thunk middleware

import { createStore, applyMiddleware } from 'redux';
import asyncThunk from 'redux-async-thunk';
import reducers from './reducers/index';

const store = createStore(
  reducers,
  applyMiddleware(asyncThunk)
);

On action

function getSomeData(){
  return async function(dispatch, getState){
    let data = await fetch('http://exampleurl.com/data');
    dispatch({type: 'MY_ACTION_TYPE', data});
  }
}

Or

function getSomeData(){
  return function* (dispatch, getState){
    let data = yield fetch('http://exampleurl.com/data');
    dispatch({type: 'MY_ACTION_TYPE', data});
  }
}

Or the original thunk way

function getSomeData(){
  return function(dispatch, getState){
    fetch('http://exampleurl.com/data').then(data => {
      dispatch({type: 'MY_ACTION_TYPE', data});
    });    
  }
}
1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.0

7 years ago