# create-shared-state

> A react hook that is like useState but state is shared instead

Latest version **1.1.0** (published 2020-08-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install create-shared-state
pnpm add create-shared-state
yarn add create-shared-state
bun add create-shared-state
```

## 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.1.0 |
| Published | 2020-08-14 |
| First published | 2020-08-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | abdul@radadi.me |
| Maintainers | radadi |
| Keywords | react, state, hook, useState, shared, global |

## Links

- npm: https://www.npmjs.com/package/create-shared-state
- Repository: https://github.com/alradadi/create-shared-state
- Homepage: https://github.com/alradadi/create-shared-state#readme
- Issues: https://github.com/alradadi/create-shared-state/issues
- npm.io page: https://npm.io/package/create-shared-state

## 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.1.0 (latest) — 2020-08-14
- 1.0.0 — 2020-08-14

## README

## create-shared-state

A hook that has the exact same API as `useState` but the state can be shared between components instead.

## Motivation
Most of the state management tools out there force you to create and initialize your shared state **outside** components and custom hooks.
Well, sometimes the initial state isn't available until the component calls another hook. The hook provided by create-shared-state can be used exactly like the default `useState` except that state is shared between components.


## Installation

```sh
yarn add create-shared-state
```

## Usage

- Example 1

```javascript
import { create } from 'create-shared-state';

const useSharedState = create();

const useCounter = () => useSharedState(0); // the custom hook useCounter can be used in any component

const [counter, setCounter] = useCounter() // use in components

```
<br>

- Example 2

```javascript
import { create } from 'create-shared-state';
import { useForm } from 'react-hook-form';


const useSharedState = create();

/**
* The logic for this form and its methods is accessible from anywhere.
* This makes it possible for one component to handle updating and validating the form,
* and another component to handle the submission for examaple. 
*/
const useOnboardingForm = () => {
  const methods = useForm()
  
  const [sharedMethods] = useSharedState(methods)

  return sharedMethods
}
```

## License

MIT

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