# react-slice

> Single dependency (react) global state. Simplicity 👌 meets performance 🔥

Latest version **0.3.1** (published 2020-10-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-slice
pnpm add react-slice
yarn add react-slice
bun add react-slice
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.1 |
| Published | 2020-10-21 |
| First published | 2019-05-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Kasper Pihl Tornoe |
| Maintainers | kasperpihl |
| Keywords | react, state management, hooks, reducer, global state, redux |

## Links

- npm: https://www.npmjs.com/package/react-slice
- npm.io page: https://npm.io/package/react-slice

## 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

- 0.3.1 (latest) — 2020-10-21
- 0.3.0 — 2020-10-21
- 0.2.4 — 2019-12-06
- 0.2.3 — 2019-12-06
- 0.2.2 — 2019-09-03
- 0.2.1 — 2019-09-03
- 0.2.0 — 2019-09-02
- 0.1.8 — 2019-07-10
- 0.1.7 — 2019-07-04
- 0.1.6 — 2019-07-04
- 0.1.5 — 2019-07-04
- 0.1.4 — 2019-07-01
- 0.1.3 — 2019-06-29
- 0.1.2 — 2019-06-29
- 0.1.1 — 2019-06-29
- … 12 more at https://npm.io/package/react-slice/versions

## README

# React Slice

A simple performant approach to state management in React.

1. Uses reducers and dispatch 🤖
2. Access state from anywhere with hooks 🚀
3. You can still [use statically](https://github.com/kasperpihl/react-slice/blob/master/docs/Statically.md) 😲
4. Optimized for [use with Typescript](https://github.com/kasperpihl/react-slice/blob/master/docs/Typescript.md) 💙
5. Full control of [render performance](https://github.com/kasperpihl/react-slice/blob/master/docs/Rerenders.md) 🔥
6. [2kb minified](https://bundlephobia.com/result?p=react-slice) 💪

## Demo

I've made a few Codesandboxes to play around with react-slice

- [Javascript sandbox](https://codesandbox.io/s/clever-worker-bpq1g)
- [Typescript sandbox](https://codesandbox.io/s/youthful-mendel-ze693)

## Installation

```js
npm i react-slice
```

## The basics

Create a file and export your hook w/ reducer (fx counterSlice.js)

```js
// counterSlice.js
import { createSlice } from 'react-slice'; // 👈👈👈

export default createSlice({
  reducer: /*👈👈👈*/ (state, action) => {
    switch (action.type) {
      case 'increment':
        return state + 1;
      default:
        return state;
    }
  },
  initialState: /*👈👈👈*/ 0,
  debugName: 'Counter', // Optional.
});
```

Then import that file somewhere else (fx App.jsx) 🏴‍☠️

```js
// App.jsx
import React from 'react';
import counterSlice from './counterSlice'; // 👈👈👈

export default function App(test) {
  const counterState = counterSlice.use(); // 👈👈👈

  const onClick = () => {
    counterSlice.dispatch({ type: 'increment' }); // 👈👈👈
  };
  return (
    <div className="App">
      <h1>{counter /*👈👈👈*/}</h1>
      <button onClick={onClick}>Increment</button>
    </div>
  );
}
```

And boom! 💥 That's all you need to get up and running!

## Advanced/FAQ

- [API for createSlice()](https://github.com/kasperpihl/react-slice/blob/master/docs/API.md)
- [Avoid unnecessary re-renders](https://github.com/kasperpihl/react-slice/blob/master/docs/Rerenders.md)
- [Use statically from utils etc.](https://github.com/kasperpihl/react-slice/blob/master/docs/Statically.md)
- [Use Typescript and get strongly typed state and actions](https://github.com/kasperpihl/react-slice/blob/master/docs/Typescript.md) 💙
- Use with Server-side rendering (coming soon, would love ideas/usecases)
- Debugging is disabled when `process.env` is 'production' or 'test'

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