# zend-redux

> 基于react-redux的封装

Latest version **1.0.0** (published 2019-12-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install zend-redux
pnpm add zend-redux
yarn add zend-redux
bun add zend-redux
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2019-12-12 |
| First published | 2019-12-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | lucian-lu |
| Keywords | react-redux, redux-hooks |

## Links

- npm: https://www.npmjs.com/package/zend-redux
- npm.io page: https://npm.io/package/zend-redux

## Recent versions

- 1.0.0 (latest) — 2019-12-12

## README

# zend-redux

基于react-redux封装，让项目目录更加清晰

## 安装

```shell script
yarn add zend-redux 或 npm i zend-redux -S 
```

## 使用

在store 入口文件使用

```javascript
import {
    createStore,
    combineReducers,
    applyMiddleware,
    compose,
  } from 'redux';
import zendRedux, { thunk } from 'zend-redux';
import app from './app'; // app模块

const reducers = zendRedux({
  app,
  // 其他模块...
});

// 开发环境下使用 redux 浏览器插件
const composeEnhancers = process.env.NODE_ENV === 'development' && 
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || 
  compose;

export default createStore(
  combineReducers(reducers),
  composeEnhancers(applyMiddleware(thunk)),
);
```

## 模块说明

每个模块必须包含**namespace**属性，完整属性如下*app*模块：

```javascript
export default {
  namespaces: 'app',
  state: {
    test: 123,
    count: 0
  },
  reducers: {
    setTest(state, payload) {
      return { // 无需返回整个state的数据，使用了扩展符浅合并
        test: payload
      };
    },
    setCount(state, payload) {
      return {
        count: payload
      }
    } 
  },
  actions: { // 异步处理
    /**
     * put {Function} 用于提交reducers，接受一个对象，对象包含type和payload属性
     * state {Object} 当前模块的state
     * dispatch {Function} react-redux的dispatch
     */
    async changeCount({ put,  state, dispatch }, payload) {
      // 异步处理
      window.setTimeout(() => {
        put({
          type: 'setCount',
          payload
        });
      }, 2000)
    }
  },
  getters: { // 获取某属性，类似vuex的getters
     test: state => state.test
  }
};
```

## API

|  属性   | 说明  | 类型 |
|  ----  | ----  | ----  |
| thunk  | 支持异步处理的中间件，参考[redux-thunk](https://github.com/reduxjs/redux-thunk/blob/master/src/index.js?_blank) 实现 | function |   
| useZendGetters  | 调用某模块的getter，获取其返回值 | function(namespace, ['test', // 其他... ]) |
| useZendReducers | 调用reducers | function(namespace, ['test', // 其他... ]) |
| useZendActions | 调用actions | function(namespace, ['test', // 其他... ]) |

## 目录结构

```.
├── src                                        
│   ├── store                                   
│   │   ├── app         // app模块                          
│   │   ├── system      // 系统管理模块                                             
│   │   └── index.js    // 入口文件              
.

```

---
_Source: https://npm.io/package/zend-redux · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
