npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-thunks

v1.0.0

Published

Thunk creator for redux

Downloads

13,086

Readme

redux-thunks

GitHub license Build Status Coverage npm Project Status

Simple thunk creator for redux.

This is meant to smooth over the use of redux-thunk and redux-actions.

Installation

$ yarn add redux-thunks redux-thunk

or

$ npm install --save redux-thunks redux-thunk

Usage

This module is simply syntactic sugar for using redux-thunk with redux-actions. As such, it requires that you use the redux-thunk middleware.

Add redux-thunk middleware

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

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

Action definition

import { createAction, handleAction } from 'redux-actions';
import { createThunk } from 'redux-thunks';

export const setAmount = createAction('SET_AMOUNT');

export const incrementAsync = createThunk('INCREMENT_ASYNC', ({ dispatch, getState }, amount) => {
  doAsyncMath(getState(), amount).then(val => dispatch(setAmount(val)));
});

// the reducer is attached to the action that is actually dispatched
export default handleAction(setAmount, (state, payload) => payload, 0);

The context also receives the type name that you gave the thunk, which is useful to using createAction inside of the handler function, so that the action type only needs to be defined once, like so:

import { createAction, handleAction } from 'redux-actions';
import { createThunk } from 'redux-thunks';

export const resetAmountAsync = createThunk('RESET_AMOUNT', ({ dispatch, type }) => {
  // reset action. type is the value provided as the action type name, 'RESET_AMOUNT'
  const action = createAction(type);

  // read value from some async source and dispatch the reset action
  fetchAsyncValue().then(val => dispatch(action(val)));
});

// the reducer can use the exported thunk action directly
export default handleAction(resetAmountAsync, (state, payload) => payload, 0);

Use in component/container

dispatch(incrementAsync, 10); // adds 10
dispatch(resetAmountAsync); // resets the value

API

createThunk

createThunk('type', ({ dispatch, getState, type }, [...args]))

The syntax is very similar to that of createAction, and it smooths over the differences by similarly exposing a toString method on the thunk creator.

createThunk takes two arguments, a type name, and a function to execute when the action is dispatched. The first argument of the function will be a context object, with the following properties:

name | description ---- | ----------- dispatch | Provided directly from redux-thunk, used to dispatch other actions getState | Provided directly from redux-thunk, used to reach the current state type | The type name of the action that was passed in as the first argument

Any additional arguments will be anything specified at the dispatch call site. It's expected that at least one other action will be dispatched from the handler function.

License

MIT © w33ble