# redux-subreducer

> Middleware for working with react-redux

Latest version **1.0.6-alpha2** (published 2018-04-22) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.6-alpha2 |
| Published | 2018-04-22 |
| First published | 2018-04-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 82.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Alexey Potsetsuev |
| Maintainers | wroud |
| Keywords | react, react-redux, redux, middleware, typescript |

## Links

- npm: https://www.npmjs.com/package/redux-subreducer
- Repository: https://github.com/Wroud/redux-subreducer
- Homepage: https://github.com/Wroud/redux-subreducer#readme
- Issues: https://github.com/Wroud/redux-subreducer/issues
- npm.io page: https://npm.io/package/redux-subreducer

## Dependencies (3)

- [react](https://npm.io/package/react.md) ^15.0.0-0 || ^16.0.0-0
- [redux](https://npm.io/package/redux.md) ^2.0.0 || ^3.0.0 || ^4.0.0-0
- [react-redux](https://npm.io/package/react-redux.md) ^5.0.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.0.6-alpha2 (latest) — 2018-04-22
- 1.0.5-alpha2 (alpha) — 2018-04-09
- 1.0.6-alpha — 2018-04-22
- 1.0.5-alpha9 — 2018-04-19
- 1.0.5-alpha8 — 2018-04-19
- 1.0.5-alpha7 — 2018-04-16
- 1.0.5-alpha6 — 2018-04-10
- 1.0.5-alpha5 — 2018-04-10
- 1.0.5-alpha4 — 2018-04-10
- 1.0.5-alpha3 — 2018-04-10
- 1.0.5-alpha — 2018-04-09
- 1.0.5 — 2018-04-08
- 1.0.4-alpha3 — 2018-04-06
- 1.0.4-alpha2 — 2018-04-06
- 1.0.4-alpha — 2018-04-06
- … 5 more at https://npm.io/package/redux-subreducer/versions

## README

# redux-subreducer

Redux-Subreducer can do:
1. deep state tree control
2. component local state control

Also provides detailed logging.
More completed example:
1. https://github.com/Wroud/ScheduleReact



[![Travis](https://img.shields.io/travis/Wroud/redux-subreducer.svg)](https://travis-ci.org/Wroud/redux-subreducer)
[![GitHub issues](https://img.shields.io/github/issues/Wroud/redux-subreducer.svg)](https://github.com/Wroud/redux-subreducer/issues)
[![GitHub license](https://img.shields.io/github/license/Wroud/redux-subreducer.svg)](https://github.com/Wroud/redux-subreducer/blob/master/LICENSE)
[![npm version](https://img.shields.io/npm/v/redux-subreducer.svg?style=flat-square)](https://www.npmjs.com/package/redux-subreducer)
[![npm downloads](https://img.shields.io/npm/dm/redux-subreducer.svg?style=flat-square)](https://www.npmjs.com/package/redux-subreducer)


## Install
```
npm install --save redux-subreducer react react-redux
```

## Documentation

* [API Reference](docs/README.md)
* [API Reference TypeScript](https://wroud.github.io/redux-subreducer/dist/docs/)

## Connect sub-reducer to redux
**1. Create sub reducer for `counter`**
```javascript
import { createAction, createRootReducer, LocalListener } from "redux-subreducer";

export const actions = {
    increment: createAction("Increment"),
    decrement: createAction("Decrement"),
};

const initalState = 0;

export const counterReducer =
    createSubReducer("counter", initalState)
        .on(actions.increment, state => state + 1)
        .on(actions.decrement, state => state - 1);
```
**2. Then create main reducer**
```javascript
import { routerReducer } from "react-router-redux";

const appInitalState = {};

export const appReducer =
    createRootReducer(appInitalState)
        .join(counterReducer)
        .joinReducer("routing", routerReducer) // also you can connect classic reducers
        .joinListener("listener", LocalListener); // or actions listener, its same reducer but not returning state
```
**3. Now create redux store**
```javascript
import { createStore } from "redux";

const store = createStore(appReducer.reducer, appInitalState);
/*
    {
        counter: 0,
        routing: {...}
    }
*/
```
**4. Dispatch actions!**
```javascript
store.dispatch(actions.increment);
/*
    {
        counter: 1,
        routing: {...}
    }
*/
store.dispatch(actions.decrement);
/*
    {
        counter: 0,
        routing: {...}
    }
*/
```

![Console log](https://i.imgur.com/BtB3wYJ.png)
## Connect component local store to `LocalReducer`
Since we connected `LocalListener` (step 2) we can connect React Component local store to `LocalReducer`

**1. Lets create one more action**
```javascript
export const switchDrawerAction = createAction("Switch Drawer");
```
**Layout.jsx**
```javascript
export class LayoutClass extends React.Component {
    render() {
        return 
            (
                <NavMenu switchDrawer={this.props.switchDrawer} />
            );
    }
}

function mapDispatchToProps(dispatch) {
  return {
    switchDrawer: () => dispatch(switchDrawerAction)
  }
}

export const Layout = connect(null, mapDispatchToProps)(LayoutClass);
```
**DrawerWraper.jsx**
```javascript
const initState = {
    open: true,
};

export class DrawerWraperClass extends React.Component {
    this.state = initState;
    render() {
        return (
            <Drawer open={this.state.open}>
                ...
            </Drawer>
        );
    }
}

const switchDrawer = (props, prevState) => ({ open: !prevState.open });

const stateReducer = reducer => reducer
    .on(switchDrawerAction, switchDrawer);


export const DrawerWrapper = connectState(
        initState,
        stateReducer,
        "drawer", // optional
    )(DrawerWraperClass);
```
**2. It works.**
![Console log](https://i.imgur.com/BE9EQXu.png)

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