0.2.2 • Published 8 years ago

redux-async-toolkit v0.2.2

Weekly downloads
25
License
MIT
Repository
github
Last release
8 years ago

redux-async-toolkit

Library that helps you write async code in redux style without need to write a lot of reducers. Simply use the one included in this library.

If you are curious about usage scroll down to 4th point.

build status npm version npm downloads

Installation

To install the current version:

npm install --save redux-async-toolkit

1. Attach reducer

import { combineReducers } from 'redux';
import { asyncReducerCreator } from 'redux-async-toolkit';

const reducer = combineReducers({
  data: asyncReducerCreator({
    hello: "some initial value"
  })
});

2. Attach middleware to your store

import { createStore, applyMiddleware } from 'redux';
import { reduxAsyncMiddleware } from 'redux-async-toolkit';

const client = {
    getHello: new Promise(resolve => setTimeout(() => resolve('hello world')));
};

const finalCreateStore = applyMiddleware(reduxAsyncMiddleware(client))(createStore);

const store finalCreateStore(reducer);

3a. Create some actions

function loadHello() {
  return {
    key: 'hello', //points to the path in the store
    async: client => client.getHello()
  };
}

3b. Cache your results if needed

function loadHello(forceReload) {
  return {
    key: 'hello', //points to the path in the store
    async: client => client.getHello(),
    cache: forceReload ? false : 10 // cache duration in seconds
  };
}

4. Connect as usual

@connect(
  state => ({
    hello: state.data.hello // your promise result is available at 'data' prop ...
  }),
  {loadHello})
export default class HelloComponent extends Component {
  static propTypes = {
    hello: PropTypes.object, // ... so you have to define its type as object here ...
    loadHello: PropTypes.func.isRequired
  };

  componentDidMount() {
    this.props.loadHello(); // ... call your action on after component mount or whenever you want ...
  }

  render() {
    const {hello} = this.props;

    return (
      <div>
        {hello.pending ? // 'pending' prop is set to true at each action start
            <div>Loading ...</div> :
            <div>
                {hello.ready && <div>{hello.data}</div>} // 'ready' means that at least one Promise was resolved
                {hello.error && <div>{hello.error}</div>} // 'error' contains data passed to Promise reject
            </div>
        }
      </div>
      );
  }
}

License

MIT

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.9

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago