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-thunk-resource

v1.1.3

Published

a library used with redux-thunk to make redux easier

Downloads

4

Readme

redux-thunk-resource

a library used with redux-thunk to make redux easier

redux-thunk-resource主要是用来减少模版代码,让redux写起来更方便,与redux-thunk结合使用。

它主要做了两件事:

  1. 将action和reducer结合,写action时直接写reducer,省去了定义action_type的麻烦。
  2. 以资源概念为核心,默认为含有异步请求的资源添加isFetching和error状态。

Build Status npm version npm downloads

Getting Started

Installation

$ npm install --save redux-actions

or

$ yarn add redux-actions

Usage

import Resource from 'redux-thunk-resource';

const resource = new Resource({ hasAddOns: true });

const setObj = resource.createAction(obj => () => ({ ...obj }));

export const fetchObj = resource.createRequest(() => async dispatch => {
    const res = await /* any fetch */ fetch({
        url: `http://www.example.com/obj`,
    });
    dispatch(setObj(res));
});

export default resource.createReducer();

Example

React-cnodejs

API

Resource

Constructor(config)

Arguments
  1. config (Object):
{
    hasAddOns: boolean // default: false
}

新建resource时,可以传入配置项,hasAddOns为true时,为资源添加isFetching、error状态。

Resource Methods

createAction
const setSearchKey = resource.createAction( searchKey => state => ({
    searchKey: { ...state.searchKey, ...searchKey },
}));

// use
dispatch(setSearchKey({
    page: 1,
    ipp: 20,
}))

上面createAction括号中searchKey是需要设置到store的参数,第二层state是此次设置前此资源的state,最后返回一个新state,会和原来state合并。

createRequest
export const fetchObj = resource.createRequest(user => async dispatch => {
    const res = await /* any fetch */ fetch({
        url: `http://www.example.com/obj`,
        data: user
    });
    dispatch(setObj(res));
});
// or
export const fetchObj = resource.createRequest(() => dispatch =>
    request({
        url: `http://www.example.com/obj`,
        mock: {
            a: 'a',
            b: 'b',
        },
    }).then(res => dispatch(setObj(res))),
);

// use
store.dispatch(fetchObj({
	id:1
}));
// or
// 可以和react-redux结合使用,connect后直接
fetchObj({
	id:1
});

createRequest会为request做一层包装, 第一层参数user是fetchObj时传入的参数,第二层开始就是redux-thunk的写法,具体请查阅此处。注意:

  1. 如上所示,无论用async或promise,返回必须是个promise。
  2. createRequest中只能包装一个请求,如果包含2个或2个以上的请求,需要将资源进行拆分。比如排行榜中需要发2个请求,一个是全部的排行榜,一个是自己的排行,需要将资源拆分成all和mine,变成2个资源,每个资源内一次dispatch只发一个请求。

当然也可以不用createRequest包装,此时写法就是redux-thunk的标准写法 注意:如果Resource构建函数中传入hasAddOns: false,则使用createRequest包装会报错。

createReducer
export default resource.createReducer(initState);

创建一个reducer,可以传入此resource的initState。