# hooksy

> [demo](https://pie6k.github.io/use-state-shared/)

Latest version **1.0.0** (published 2019-06-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install hooksy
pnpm add hooksy
yarn add hooksy
bun add hooksy
```

## 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.0.0 |
| Published | 2019-06-22 |
| First published | 2019-06-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 131 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | pie6k |

## Links

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

## Dependencies (2)

- [react](https://npm.io/package/react.md) ^16.0.0
- [react-dom](https://npm.io/package/react-dom.md) ^16.8.2

## Recent versions

- 1.0.0 (latest) — 2019-06-22

## README

# use-state-shared

[demo](https://pie6k.github.io/use-state-shared/)

Create custom `useState` hook that can be used inside multiple components and will share state across all of them (and will update all of them if state is changed)


## API

```ts
import { createSharedStateHook } from 'use-state-shared';

// create custom `useState` hook and set default value only once. 
export const customUseState = createSharedStateHook(0);

// later import `customUseState` anywhere in the app

// use the same way as `useState` inside multiple different components. 

// `currentState` will be always synced between all of them. 
// changing it in any component will cause change in every component using it with updated value
const [currentState, setState] = customUseState();
```


## Example

```tsx
import { createSharedStateHook } from 'use-state-shared';

// create custom state hook with some initial value
// such hook can be created in some separated file and imported to many different components
const useGlobalCount = createSharedStateHook(0);
const useAnotherGlobalCount = createSharedStateHook(0);

function ComponentA() {
  const [globalCount, setGlobalCount] = useGlobalCount();
  const [anotherGlobalCount] = useAnotherGlobalCount();

  return (
    <div>
      I'm first component and count is {globalCount}. 2nd count is{' '}
      {anotherGlobalCount}.
      <button onClick={() => setGlobalCount(globalCount + 1)}>Increase</button>
    </div>
  );
}

function ComponentB() {
  const [globalCount, setGlobalCount] = useGlobalCount();
  const [anotherGlobalCount, setAnotherGlobalCount] = useAnotherGlobalCount();

  return (
    <div>
      I'm second component. Count is {globalCount}. This count will always be
      synced with ComponentA count.
      <button onClick={() => setGlobalCount(globalCount - 1)}>Decrease</button>
      <button onClick={() => setAnotherGlobalCount(anotherGlobalCount + 1)}>
        Increase 2nd count
      </button>
    </div>
  );
}
```

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