# redux-custom

> 封装的useReducer模拟Redux库

Latest version **1.1.15** (published 2020-11-30) · MIT license · 0 weekly downloads

## Install

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

## 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.1.15 |
| Published | 2020-11-30 |
| First published | 2019-08-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 10.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | strliu |
| Maintainers | strliu |
| Keywords | TypeScript, React, JavaScript |

## Links

- npm: https://www.npmjs.com/package/redux-custom
- Repository: https://github.com/StringLiu2/hooks-useReducer-integration
- Issues: https://github.com/StringLiu2/hooks-useReducer-integration/issues
- npm.io page: https://npm.io/package/redux-custom

## Dependencies (1)

- [react](https://npm.io/package/react.md) ^16.9.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.15 (latest) — 2020-11-30
- 1.1.14 — 2020-11-30
- 1.1.8 — 2019-08-23
- 1.1.7 — 2019-08-23
- 1.1.6 — 2019-08-23
- 1.1.5 — 2019-08-15
- 1.1.4 — 2019-08-15
- 1.1.3 — 2019-08-15
- 1.1.2 — 2019-08-15
- 1.1.1 — 2019-08-15
- 1.1.0 — 2019-08-15
- 1.0.7 — 2019-08-15
- 1.0.6 — 2019-08-15
- 1.0.5 — 2019-08-15

## README

#基于React的createContext, useContext, useReducer封装的组合reducer库

#### 源码都在src下面,需要的可以去github拿也可以，多谢支持
[github仓库地址:https://github.com/StringLiu2/hooks-useReducer-integration](https://github.com/StringLiu2/hooks-useReducer-integration)
## 库代码如下:
``` ts
import React, { Context, createContext, useContext, useReducer } from 'react';
/** 
 * @createContext ProviderContext 一个创建完毕的context
*/
const ProviderContext: Context<any> = createContext('provider');
/** 高阶函数
 *  @param reducer 传入一个整合或者没有整合的reducer
 *  @param initialState 传入一个初始化的state，用来当做默认值
 */
export default (reducer: Function, initialState: any) => (Com: React.FC | React.ComponentClass) => {
    return () => {
        const [state, dispatch] = useReducer<any>(reducer, initialState);
        return (
            <ProviderContext.Provider value={{ state, dispatch }}>
                <Com />
            </ProviderContext.Provider >
        );
    }
}
/**
 *      获取自定义的redux
 *  @state 这个参数是需要拿来使用的state 
 *  @dispatch 这个参数是dispatch分发action使用的
 *  @T 接收一个state的类型，是总体的state的类型
 */
export function useCustomRedux<T>() {
    interface Type {
        dispatch: Dispatch<T>;
        state: T;
    }
    return useContext<Type>(ProviderContext);
}

/**接受一个包含多个reducer函数的对象，返回一个新的reducer函数
 * @param reducers 传入多个reducer,用于整合成一个reducer
 */
export function combineReducers(reducers: any) {//整合reducer函数的对象的函数
    return function (state = {}, action: any) {//返回一个整合之后的reducer函数 ,然后传给了createStore使用
        //依次调用所有的reduce函数，并得到了状态,然后得到了许多的状态,得到n个新的子状态，封装成对象并返回
        return Object.keys(reducers).reduce((newState: any, key: string) => {
            newState[key] = reducers[key]((state as any)[key], action);//然后得到新的子状态，赋值给对应的key的新state里面
            return newState;
        }, {});
    }
}
```
## 使用方式: 
### reducer.ts文件
``` ts
import { combineReducers } from './redux-custom';
const initialReucer1 = {
    count: 1
}
const initialReucer2 = {
    user: {}
}
function reducer1(state = initialReucer1, action: any) {
    switch (action.type) {
        case 'ADD_COUNTER':
            return { ...state, count: state.count + 1 }
        default:
            return state;
    }
}
function reducer2(state = initialReucer2, action: any) {
    switch (action.type) {
        default:
            return state;
    }
}

export const reducer = combineReducers({ reducer1, reducer2 });//合并reducer
export const initialState = { reducer1: initialReucer1, reducer2: initialReucer2 };//合并initialState
```
### test.tsx
```ts
import React from 'react'
import provider, { useCustomRedux } from './redux-custom';
import { reducer, initialState } from './reducer';
function Test() {
    const { state, dispatch } = useCustomRedux();
    return (
        <div>
            <h2>{state.reducer1.count}</h2>
            <button onClick={() => dispatch({ type: 'ADD_COUNTER' })}>触发dispatch-action</button>
        </div>
    )
}
export default provider(reducer, initialState)(Test);
```

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