# mini-store

> [![Travis](https://img.shields.io/travis/yesmeck/mini-store.svg?style=flat-square)](https://travis-ci.org/yesmeck/mini-store)

Latest version **3.0.6** (published 2020-07-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install mini-store
pnpm add mini-store
yarn add mini-store
bun add mini-store
```

## 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 | 3.0.6 |
| Published | 2020-07-31 |
| First published | 2017-10-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 48.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 100 |
| Maintainers | yesmeck |

## Links

- npm: https://www.npmjs.com/package/mini-store
- Repository: https://github.com/yesmeck/mini-store
- Issues: https://github.com/yesmeck/mini-store/issues
- npm.io page: https://npm.io/package/mini-store

## Dependencies (2)

- [shallowequal](https://npm.io/package/shallowequal.md) ^1.0.2
- [hoist-non-react-statics](https://npm.io/package/hoist-non-react-statics.md) ^3.3.2

## Recent versions

- 3.0.6 (latest) — 2020-07-31
- 3.0.5 — 2020-06-02
- 3.0.4 — 2020-05-27
- 3.0.3 — 2020-05-06
- 3.0.2 — 2020-04-13
- 3.0.0 — 2020-03-27
- 3.0.1 — 2020-03-27
- 2.0.0 — 2018-10-12
- 1.1.2 — 2018-08-13
- 1.1.1 — 2018-08-10
- 1.1.0 — 2018-04-19
- 1.0.4 — 2018-01-08
- 1.0.3 — 2017-11-17
- 1.0.2 — 2017-10-23
- 1.0.1 — 2017-10-21
- … 1 more at https://npm.io/package/mini-store/versions

## README

# mini-store

[![Travis](https://img.shields.io/travis/yesmeck/mini-store.svg?style=flat-square)](https://travis-ci.org/yesmeck/mini-store)

A state store for React component.

## Motivation

When you want to share a component's state to another one, a commom pattern in React world is [lifting state up](https://reactjs.org/docs/lifting-state-up.html#lifting-state-up). But one problem of this pattern is performance, assume we have a component in following hierarchy:

```javascript
<Parent>
  <ChildA />
  <ChildB />
  <ChildC />
</Parent>
```

`ChildA` want to share state with `ChildB`, so you lifting `ChildA`'s state up to `Parent`. Now, when `ChildA`'s state changes, the whole `Parent` will rerender, includes `ChildC` which should not happen.

Redux do a good job at this situation throgh keeping all state in store, then component can subscribe state's changes, and only connected components will rerender. But `redux` + `react-redux` is overkill when you are writing a component library. So I wrote this little library, It's like Redux's store without "reducer" and "dispatch".

## Example

[See this demo online.](https://codesandbox.io/s/mq6223x08p)

```javascript
import { Provider, create, connect } from 'mini-store';

class Counter extends React.Component {
  constructor(props) {
    super(props);

    this.store = create({
      count: 0,
    });
  }

  render() {
    return (
      <Provider store={this.store}>
        <div>
          <Buttons />
          <Result />
        </div>
      </Provider>
    )
  }
}

@connect()
class Buttons extends React.Component {
  handleClick = (step) => () => {
    const { store } = this.props;
    const { count } = store.getState();
    store.setState({ count: count + step });
  }

  render() {
    return (
      <div>
        <button onClick={this.handleClick(1)}>+</button>
        <button onClick={this.handleClick(-1)}>-</button>
      </div>
    );
  }
}

@connect((state) => ({ count: state.count }))
class Result extends React.Component {
  render() {
    return (
      <div>{this.props.count}</div>
    );
  };
}
```

## API

### `create(initialState)`

Creates a store that holds the state. `initialState` is plain object.

### `<Provider store>`

Makes the store available to the connect() calls in the component hierarchy below.

### `connect(mapStateToProps)`

Connects a React component to the store. It works like Redux's `connect`, but only accept `mapStateToProps`. The connected component also receive `store` as a prop, you can call `setState` directly on store.

## License

MIT

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