# react-reducer-thunk

> react hooks useReducer for asynchronous processing

Latest version **1.0.1** (published 2019-05-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install react-reducer-thunk
pnpm add react-reducer-thunk
yarn add react-reducer-thunk
bun add react-reducer-thunk
```

## 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.1 |
| Published | 2019-05-29 |
| First published | 2019-05-29 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | xfz1987 |
| Maintainers | xfz1987 |
| Keywords | react, hooks, useReducer, useContext, async |

## Links

- npm: https://www.npmjs.com/package/react-reducer-thunk
- Repository: https://github.com/xfz1987/react-reducer-thunk
- Homepage: https://github.com/xfz1987/react-reducer-thunk#readme
- Issues: https://github.com/xfz1987/react-reducer-thunk/issues
- npm.io page: https://npm.io/package/react-reducer-thunk

## Dependencies (1)

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

## 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.1 (latest) — 2019-05-29
- 1.0.0 — 2019-05-29

## README

# react-reducer-thunk
> - Base on react(16.8.*) with react-hooks

## Why use this?
At present.useReducer can not process asynchronous request.We can only do synchronous things.
So we can use useContext and some skills to process the question. Below

## Installation
`npm install --save react-reducer-thunk`

## Usage
**Example with defaults**
Creating an example reducer
```bash
import { getProvider, Context } from 'react-reducer-thunk'

const initialState = {
  isOnline: false,
  timeDuration: '00:00:00',
}

const reducer = (state, { type, payload }) => {
  switch (type) {
    case 'getLoginStatus':
      return { ...state, isOnline: payload === 1 }
    case 'changeTime':
      return { ...state, timeDuration: payload.time }
    default:
      return state
  }
}

const Provider = getProvider(reducer, initialState)

export { 
  Context,
  Provider
}
```

Creating an example component
```bash
// business component
import React, { useState, useEffect, useContext } from 'react'
import { Provider, Context } from './reducer.js'

const getStatus = async () => {
  const data = await asyncFunc()
  return data
}

function asyncFunc() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(1)
    }, 2000)
  })
}


const User = props => {
  const { state, dispatch } = useContext(Context)

  useEffect(() => {
    dispatch({
      type: 'getLoginStatus',
      payload: getStatus()
    })
  }, [])

  const changeTime = () => {
    dispatch({
      type: 'changeTime',
      payload: { time: '11:11:11' }
    })
  }

  return (
    <div className="im-bar cursor-pointer inline-block">
      <div>{state.isOnline ? '已登录': '未登录'}</div>
      <span className="time" onClick={changeTime}>{state.timeDuration}</span>
    </div>
  )
}ß

export default props => {
  return (
    <Provider>
      <User {...props} />
    </Provider>
  )
}
```

## Instructions
> - params: your own reducer and initialState
> - export Context and Provider
> - use Provider to wrapped your component
> - `useContext(Context)` for get state and dispatch
> - dispatch({ type, payload }) - payload: async func or data

## License
ISC

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