# react-store-module

> Centralized State Management easily with hooks for React Applications.

Latest version **1.0.2** (published 2020-12-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-store-module
pnpm add react-store-module
yarn add react-store-module
bun add react-store-module
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-12-25 |
| First published | 2020-12-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | yonechen |
| Maintainers | yonechen |
| Keywords | react, store, hook, vuex |

## Links

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

## 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.2 (latest) — 2020-12-25
- 1.0.1 — 2020-12-25
- 1.0.0 — 2020-12-25

## README

# react-store-module

> Centralized State Management with hooks for React Applications.

## Install

```sh
npm i react-store-module
```

## Example

This example shows how to change global theme color by use `react-store-module`

### Create a StoreModule

```jsx
// store/theme.js
const storeModule = {
  name: 'theme',
  state: { // initial states
    color: '#fff'
  },
  actions: {
    CHANGE_COLOR(state, payload) {
      return {
        ...state,
        addLabel: payload,
      };
    },
  },
};

export default storeModule;

```

### Import StoreProvider Component

Add `StoreProvider Component` at the root of React Nodes, and pass your storeModule list.

```jsx
// index.js
import React from 'react';
import ReactDOM from 'react-dom';

import StoreProvider from 'react-store-module';
import ThemeStoreModule from '@/store/theme';

import HeaderBar from '@/components/header-bar';
import Home from '@/views/home';

ReactDOM.render(
  <StoreProvider storeModules={[ThemeStoreModule]}>
    <HeaderBar>
    <Home/>
  </StoreProvider>,
  document.getElementById('app'),
);
```

This step will create the global store context.

### `useStore` to get states and dispatch actions

When called `useStore` by passing module name, it will return `{ state, dispatch }` of the store.

```jsx
// views/home.js
import { useStore } from 'react-store-module';
function Home() {
  const { state } = useStore('theme');
  return <div style={{ backgroundColor: state.color }}><div/>
}
export default Home
```

Now, we can `Dispatch` our actions to change the States, like change the theme color.

```jsx
// components/header-bar.js
import { useStore } from 'react-store-module';

function getRandomColor() {
  const letters = '0123456789ABCDEF';
  let color = '#';
  for (let i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)];
  }
  return color;
}
function HeaderBar() {
  const { dispatch } = useStore('theme');
  return <header>
    <button onClick={() => {
      dispatch('CHANGE_COLOR', getRandomColor())
    } }></button>
  </header>
}
export default HeaderBar
```

The changed state.color will trigger component to rerender.

## TODO

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