# state-tool

> 利用 react hooks 特性来做状态管理，功能比较简单

Latest version **1.0.0** (published 2020-03-29) · 0 weekly downloads

## Install

```sh
npm install state-tool
pnpm add state-tool
yarn add state-tool
bun add state-tool
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2020-03-29 |
| First published | 2020-03-29 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | phoeshow.me |

## Links

- npm: https://www.npmjs.com/package/state-tool
- npm.io page: https://npm.io/package/state-tool

## Recent versions

- 1.0.0 (latest) — 2020-03-29

## README

# state-tool

利用 react hooks 特性来做状态管理，功能比较简单

## 使用方法

### 简单使用

创建一个 store.js 文件，需要有 initState 和 reducer 函数

```javascript
// store.js
import StateTool from 'state-tool';
// 可以通过设置 StateTool 的 debug 属性来开启日志输出
// StateTool.debug = true

const initState = {
  count: 0
};
const reducer = (state = initState, action) => {
  switch (action.type) {
    case 'add':
      return { ...state, age: state.count + 1 };
    default:
      return state;
  }
};

export const { Provider, useDispatch, useStore } = StateTool.createStore(
  initState,
  reducer
);

// index.js
import Provider from './store';

ReactDOM.render(
  <React.StrictMode>
    <Provider>
      <App />
    </Provider>
  </React.StrictMode>,
  document.getElementById('root')
);

// App.js
import React from 'react';

import {
  useStore as useCountStore,
  useDispatch as useCountDispatch
} from './store';

function App() {
  const dispatchCount = useCountDispatch();
  return (
    <div>
      <button
        onClick={() => {
          dispatch({ type: 'add' });
        }}
      >
        add
      </button>
      <Age />
    </div>
  );
}

const Age = props => {
  const store = useCountStore();
  return <div>count is: {store.count}</div>;
};
export default App;
```

合并多个 store

```javascript
import StateTool from 'state-tool';
import { Provider as Provider1 } from 'store1.js';
import { Provider as Provider2 } from 'store2.js';
const Provider = StateTool.combindProvider(Provider1, Provider2);
```

## License

[MIT](LICENSE).

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