All Projects → machadogj → redux-thunk-actions

machadogj / redux-thunk-actions

Licence: MIT license
Action creator for redux-thunk that handles sync and async functions.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to redux-thunk-actions

fetch-action-creator
Fetches using standardized, four-part asynchronous actions for redux-thunk.
Stars: ✭ 28 (-55.56%)
Mutual labels:  redux-thunk, redux-actions
React Demo Store
Moltin + React powered online store
Stars: ✭ 198 (+214.29%)
Mutual labels:  redux-thunk
Redux Most
Most.js based middleware for Redux. Handle async actions with monadic streams & reactive programming.
Stars: ✭ 137 (+117.46%)
Mutual labels:  redux-thunk
Slopeninja Native
 Slope Ninja App 🎿❄️⛄️
Stars: ✭ 160 (+153.97%)
Mutual labels:  redux-thunk
React Admin
基于antd、redux-observable、redux-thunk、react-router响应式SPA脚手架,后台管理系统demo. 权限管理,用户管理,菜单管理。无限级菜单,下拉树形选择框
Stars: ✭ 141 (+123.81%)
Mutual labels:  redux-thunk
Create React Redux App Structure
Create React + Redux app structure with build configurations ✨
Stars: ✭ 161 (+155.56%)
Mutual labels:  redux-thunk
Frontend Bootcamp
Frontend Workshop from HTML/CSS/JS to TypeScript/React/Redux
Stars: ✭ 10,506 (+16576.19%)
Mutual labels:  redux-thunk
Music163 React
🔥基于React全家桶开发:「网易云音乐PC端项目」实战
Stars: ✭ 209 (+231.75%)
Mutual labels:  redux-thunk
Noder React Native
The mobile app of cnodejs.org written in React Native
Stars: ✭ 1,995 (+3066.67%)
Mutual labels:  redux-thunk
React Native Typescript Boilerplate
React Native Typescript starter kit / template (Redux Thunk + React Native Navigation v7 + TSLint)
Stars: ✭ 155 (+146.03%)
Mutual labels:  redux-thunk
React Admin
基于[email protected]的react动态权限后台管理系统模板
Stars: ✭ 151 (+139.68%)
Mutual labels:  redux-thunk
React Redux Architecture
Learn how to architect a React Redux application in a classy way
Stars: ✭ 148 (+134.92%)
Mutual labels:  redux-thunk
Firebase React Native Redux Starter
Starter For Firebase, React Native, Redux Applications With 100% Of Code In Common Between IOS And Android, with built In Authentication, Crud Example And Form Validation.
Stars: ✭ 166 (+163.49%)
Mutual labels:  redux-thunk
Kea
Production Ready State Management for React
Stars: ✭ 1,805 (+2765.08%)
Mutual labels:  redux-thunk
Mern Ecommerce
🎈 Fullstack MERN Ecommerce Application
Stars: ✭ 205 (+225.4%)
Mutual labels:  redux-thunk
Typescript React Starter
🚀 TypeScript [ React + React-Router + Redux + Redux-Thunk ] Starter
Stars: ✭ 132 (+109.52%)
Mutual labels:  redux-thunk
React Interview Questions
300+ React Interview Questions
Stars: ✭ 151 (+139.68%)
Mutual labels:  redux-thunk
Space
A real time chat app for developers built using React, Redux, Electron and Firebase
Stars: ✭ 161 (+155.56%)
Mutual labels:  redux-thunk
React Redux Firebase
Redux bindings for Firebase. Includes React Hooks and Higher Order Components.
Stars: ✭ 2,492 (+3855.56%)
Mutual labels:  redux-thunk
Redux Saga Thunk
Dispatching an action handled by redux-saga returns promise
Stars: ✭ 212 (+236.51%)
Mutual labels:  redux-thunk

redux-thunk-actions

Easily create action creators for redux with redux-thunk.

Rationale

With redux-actions you can do:

let increment = createAction('INCREMENT');
expect(increment(42)).to.deep.equal({
  type: 'INCREMENT',
  payload: 42
});

With redux-thunk you can do:

function myFetch() {
  // instead of an object, you can return a function
  return (dispatch) => {
    dispatch({type: 'MY_FETCH_START'});
    try {
      //we can do async and then dispatch more stuff
      await api.fetch();
    }
    catch(e) {
      return dispatch({type: 'MY_FETCH_FAIL', error: e});
    }
    dispatch({type: 'MY_FETCH_END'});
  }
}
dispatch(myFetch());

With redux-thunk-actions, you can do:

let myFetch = createActionThunk('MY_FETCH', () => api.fetch());

This will generate two of three possible actions:

  • MY_FETCH_STARTED
  • MY_FETCH_SUCCEEDED
  • MY_FETCH_FAILED
  • MY_FETCH_ENDED

You can pass both sync and async functions and the actions will be dispatched accordingly.

Installation

npm install --save redux-thunk-actions

Usage

import { createActionThunk } from 'redux-thunk-actions';

non-async

With non async functions, it will dispatch start/fail/end actions anyway.

reducer.js

case 'FETCH_SUCCEEDED':
  return Object.assign({}, state, {
    data: action.payload
  });

You can dispatch as usual:

let fetch = createActionThunk('FETCH', () => 3);
dispatch(fetch());
assert.equal(store.getState().data, 3);

async

let fetch = createActionThunk('FETCH', myAsyncFunc);
// you can try/catch dispatch.
let data = await dispatch(fetch());

With promises:

let fetch = createActionThunk('FETCH', myAsyncFunc);
dispatch(fetch()).then(
  data => {
    console.log(data)
    //state is already updated!
    assert.equal(store.getState().data, data);
  },
  error => console.log(error)
);

Errors

reducer.js

//...
    case 'FETCH_FAILED':
      return Object.assign({}, state, {
        started: false,
        error: action.error
      });

then if the action throws it fails:

    let fetch = createActionThunk('FETCH', () => {
      throw new Error('boom!');
    });
    try {
      //if action is async, you can use await here!
      dispatch(fetch());
    }
    catch(e) {
      assert.equal(e.message, 'boom!');
      assert.equal(getState().error, true);
    }

if you want to suppress throwing exceptions and instead handle errors 100% in the reducers, pass true as the 3rd argument

const action = createActionThunk('FETCH', () => 3, true)

Metadata

Sometimes you want to send metada with your actions

To do so just return an object with payload and meta. This properties will be used to generate those values:

let fetch = createActionThunk('FETCH', () => ({payload: 3, meta: 4}));
dispatch(fetch());
// will dispatch: 
// {type: FETCH_START});
// {type: FETCH_SUCCEEDED, payload: 3, meta: 4});
// {type: FETCH_ENDED, payload: elapsed: 1});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].