# @alekna/react-store

> A redux like store managed by rxjs.

Latest version **1.2.12-alpha.0** (published 2020-02-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @alekna/react-store
pnpm add @alekna/react-store
yarn add @alekna/react-store
bun add @alekna/react-store
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.12-alpha.0 |
| Published | 2020-02-27 |
| First published | 2019-06-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 18.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | David Alekna |
| Maintainers | alekna |
| Keywords | react, forms, form, streams, rxjs, generators |

## Links

- npm: https://www.npmjs.com/package/@alekna/react-store
- Repository: https://github.com/davidalekna/alekna
- Homepage: https://github.com/davidalekna/alekna#readme
- Issues: https://github.com/davidalekna/alekna/issues
- npm.io page: https://npm.io/package/@alekna/react-store

## Dependencies (3)

- [rxjs](https://npm.io/package/rxjs.md) ^6.5.3
- [lodash.merge](https://npm.io/package/lodash.merge.md) ^4.6.2
- [lodash.clonedeep](https://npm.io/package/lodash.clonedeep.md) ^4.5.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.2.12-alpha.0 (latest) — 2020-02-27
- 1.2.11-alpha.0 — 2020-02-26
- 1.2.10-alpha.0 — 2020-02-25
- 1.2.9-alpha.0 — 2020-02-24
- 1.2.8-alpha.0 — 2020-02-24
- 1.2.7-alpha.0 — 2020-02-24
- 1.2.6 — 2020-01-31
- 1.2.5 — 2020-01-27
- 1.2.4 — 2020-01-27
- 1.2.1 — 2020-01-20
- 1.1.0 — 2019-09-04
- 1.0.2 — 2019-08-26
- 1.0.1 — 2019-08-26
- 1.0.0 — 2019-06-18

## README

<div align="center">
<h1>@alekna/react-store</h1>

<p>async redux type store for react with rxjs touch.</p>

<p style="text-align: center;">

[![size][size-badge]][unpkg-lib]
[![gzip size][gzip-badge]][unpkg-lib]

</p>
</div>
<hr />

<pre>npm i <a href="https://www.npmjs.com/package/@alekna/react-store">@alekna/react-store</a></pre>

## Usage

> [Example](https://codesandbox.io/s/aleknareact-store-hooks-8fgiw)

### useAsyncReducer

> [Try it out in the browser](https://codesandbox.io/s/determined-borg-l7egr)

Works just like react useReducer, but dispatcher can also handle promises and observables

```jsx
import React from 'react';
import { render } from 'react-dom';
import { interval } from 'rxjs';
import { map, take, startWith } from 'rxjs/operators';
import { useAsyncReducer } from '@alekna/react-store';

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

function reducer(state = { count: 0 }, action) {
  switch (action.type) {
    case 'increment':
      return { count: state.count + 1 };
    case 'decrement':
      return { count: state.count - 1 };
    default:
      return state;
  }
}

const App = () => {
  const [state, dispatch] = useAsyncReducer(reducer);
  return (
    <>
      <div>Count: {state.count}</div>
      <div>
        <button
          style={{ padding: '10px 20px', fontSize: 24 }}
          onClick={() =>
            dispatch(async () => {
              await sleep(500);
              return { type: 'increment' };
            })
          }
        >
          +
        </button>
        <button
          style={{ padding: '10px 20px', fontSize: 24 }}
          onClick={() => dispatch({ type: 'decrement' })}
        >
          -
        </button>
        <button
          style={{ padding: '10px 20px', fontSize: 24 }}
          onClick={() =>
            dispatch(() => {
              return interval(1000).pipe(
                startWith(0),
                take(5),
                map(() => ({
                  type: 'increment',
                })),
              );
            })
          }
        >
          auto click
        </button>
      </div>
    </>
  );
};

render(<App />, document.getElementById('root'));
```

### Usage with hooks api

> [Try it out in the browser](https://codesandbox.io/s/aleknareact-store-hooks-754lm)

```jsx
import React from 'react';
import { render } from 'react-dom';
import { of } from 'rxjs';
import { delay } from 'rxjs/operators';
import { useStore, createStore } from '@alekna/react-store';

const onChange = text => () => {
  return of({
    type: 'onChange',
    payload: text,
  }).pipe(delay(1000));
};

function reducer(state = { text: '' }, action) {
  switch (action.type) {
    case 'onChange':
      return {
        ...state,
        text: state.text.concat(action.payload.slice(-1)),
      };
    default:
      return state;
  }
}
const storeConfig = createStore(reducer);

function App() {
  const { state, dispatch } = useStore(storeConfig);

  return (
    <div style={{ display: 'flex', flexDirection: 'column' }}>
      <h1>Start typing bellow</h1>
      <input
        value={state.text}
        placeholder="your input will be delayed by 1sec"
        onChange={evt => dispatch(onChange(evt.target.value))}
      />
    </div>
  );
}

render(<App />, document.getElementById('root'));
```

### Usage with render props and context api

> [Try it out in the browser](https://codesandbox.io/s/aleknareact-store-render-props-3dfcm)

```jsx
import React from 'react';
import { render } from 'react-dom';
import { of } from 'rxjs';
import { delay } from 'rxjs/operators';
import StoreProvider, { createStore, useSelector } from '@alekna/react-store';

const onChange = text => () => {
  return of({
    type: 'onChange',
    payload: text,
  }).pipe(delay(1000));
};

function reducer(state = { text: '' }, action) {
  switch (action.type) {
    case 'onChange':
      return {
        ...state,
        text: state.text.concat(action.payload.slice(-1)),
      };
    default:
      return state;
  }
}
const storeConfig = createStore(reducer);

function App() {
  return (
    <StoreProvider store={storeConfig}>
      {({ dispatch }) => (
        <div style={{ display: 'flex', flexDirection: 'column' }}>
          <h1>Start typing bellow</h1>
          <Input dispatch={dispatch} />
        </div>
      )}
    </StoreProvider>
  );
}

function Input({ dispatch }) {
  const text = useSelector(state => state.text);
  return (
    <input
      value={text}
      placeholder="your input will be delayed by 1sec"
      onChange={evt => dispatch(onChange(evt.target.value))}
    />
  );
}

render(<App />, document.getElementById('root'));
```

[gzip-badge]: http://img.badgesize.io/https://unpkg.com/downshift/dist/downshift.umd.min.js?compression=gzip&label=gzip%20size&style=flat-square
[size-badge]: http://img.badgesize.io/https://unpkg.com/downshift/dist/downshift.umd.min.js?label=size&style=flat-square
[unpkg-lib]: https://unpkg.com/@alekna/react-store@1.0.5/lib

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