# @maple-rc/redux-hook

> Use hooks realize redux in react

Latest version **1.0.3** (published 2021-04-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install @maple-rc/redux-hook
pnpm add @maple-rc/redux-hook
yarn add @maple-rc/redux-hook
bun add @maple-rc/redux-hook
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2021-04-23 |
| First published | 2021-04-21 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 9 |
| Unpacked size | 16.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | mapleliu1@gmail.com |
| Maintainers | mapleliu |
| Keywords | react, hook, hooks, redux |

## Links

- npm: https://www.npmjs.com/package/@maple-rc/redux-hook
- Repository: https://github.com/liujian10/redux-hook
- Homepage: http://./
- Issues: https://github.com/liujian10/redux-hook/issues
- npm.io page: https://npm.io/package/@maple-rc/redux-hook

## Dependencies (9)

- [react](https://npm.io/package/react.md) ^17.0.1
- [react-dom](https://npm.io/package/react-dom.md) ^17.0.1
- [web-vitals](https://npm.io/package/web-vitals.md) ^1.0.1
- [@types/react](https://npm.io/package/@types/react.md) ^17.0.2
- [react-scripts](https://npm.io/package/react-scripts.md) 4.0.3
- [@types/webpack-env](https://npm.io/package/@types/webpack-env.md) ^1.16.0
- [@testing-library/react](https://npm.io/package/@testing-library/react.md) ^11.1.0
- [@testing-library/jest-dom](https://npm.io/package/@testing-library/jest-dom.md) ^5.11.4
- [@testing-library/user-event](https://npm.io/package/@testing-library/user-event.md) ^12.1.10

## 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.0.3 (latest) — 2021-04-23
- 1.0.1 — 2021-04-21

## README

# redux-hook

 [![npm version](https://img.shields.io/npm/v/@maple-rc/redux-hook.svg?style=flat)](https://www.npmjs.com/package/@maple-rc/redux-hook) [![Build Status](https://travis-ci.org/liujian10/redux-hook.svg?branch=master)](https://travis-ci.org/liujian10/redux-hook)

Use hooks realize redux in react

---

## 安装

```
    yarn add @maple-rc/redux-hook
```

---

## 使用

### Provider

容器，传入 `reducer` 及 `initState`，生成全局 `Store`

用法

```jsx

<Provider reducer={reducer} initState={initState}>
  {children}
</Provider>

```

入参

| 参数    | 说明         | 类型                                                 |
|---------|--------------|------------------------------------------------------|
|reducer|reducer值|`object`|
|initState|初始state|`object`|

示例

```jsx

import Demo from './demo/index'
import { Provider, combineReducers } from './hooks'

import { reducers, initState } from './demo/reduces'

const App = () => {
  const combinedReducer = combineReducers(reducers)
  return (
    <Provider reducer={combinedReducer} initState={initState}>
      <Demo />
    </Provider>
  )
}

export default App

```

---

### combineReducers

将拆分的多个 `reducer`，合并一个最终的 `reducer`

用法

```jsx
combineReducers(reducers)
```

入参

| 参数    | 说明         | 类型                                                 |
|---------|--------------|------------------------------------------------------|
|reducers|reducer键值对|`object`|

示例

```jsx

const reducers = {
  todos: (state: ITodos, action: IAction) => {
    switch (action.type) {
      case 'CHANGE_TEXT':
        return {
          ...state,
          text: action.payload,
        }
      default:
        return state
    }
  },
  counter: (state: ICounter, action: IAction) => {
    switch (action.type) {
      case 'CHANGE_COUNT':
        return {
          ...state,
          count: action.payload,
        }
      default:
        return state
    }
  }
}

combineReducers(reducers)

```

```jsx

// 最终的 reducer 伪代码
(state: Object = {}, action: IAction) => {
  switch (action.type) {
    case 'CHANGE_TEXT':
      return {
        ...state,
        text: action.payload,
      }
    case 'CHANGE_COUNT':
      return {
        ...state,
        count: action.payload,
      }
    default:
      return state
  }
}
```

---

### connect

连接页面与 `Store`，切分并注入 `Store state`

用法

```javascript
connect([selecter], [mergeProps])
```

入参

| 参数    | 说明         | 类型                                                 |
|---------|--------------|------------------------------------------------------|
|selecter|`Store state` 切分函数|`func`|
|mergeProps|组合 `Store state` 到 `props`|`func`|

---

示例

```jsx

import { Dispatch } from 'react'
import connect from '../hooks/connect'
import { IAction, IState } from '../hooks/types'
import * as actions from './actions'

interface IProps {
  todos: {
    text: string
  }
  counter: {
    count: number
  }
  dispatch: Dispatch<IAction>
}

const Todos = (props: IProps) => {
  // 用 useContext 来获取 state 与 dispatch
  const { todos, counter, dispatch } = props
  const change = (val: string) => dispatch(actions.changeText(val))
  const increment = () => dispatch(actions.increment(counter.count))
  console.log('todos', todos)

  return (
    <div>
      <h1>The text is {todos.text}</h1>
      <button onClick={() => change('new')}>change</button>
      <button onClick={increment}>increment</button>
    </div>
  )
}

export default connect(
  (state: IState) => state.counter, // selector
  (state: IState) => state.todos, // selector
  (counter: IState, todos: IState) => ({ counter, todos }) // mergeProps
)(Todos)

```

### useDispatch

获取 `dispatch`

用法

```javascript
useDispatch()
```

回参

| 参数    | 说明         | 类型                                                 |
|---------|--------------|------------------------------------------------------|
|useDispatch|`dispatch` 函数|`func`|

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