# react-happy-global-state

> Easy to manage your global state, just like using the useState hook.

Latest version **0.1.4** (published 2024-09-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-happy-global-state
pnpm add react-happy-global-state
yarn add react-happy-global-state
bun add react-happy-global-state
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.4 |
| Published | 2024-09-07 |
| First published | 2023-06-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | easilyjs |
| Maintainers | easilyjs |
| Keywords | react, hooks, global, state, global-state, redux, typescript, react-hooks |

## Links

- npm: https://www.npmjs.com/package/react-happy-global-state
- Repository: https://github.com/react-earth/react-happy-global-state
- Homepage: https://github.com/react-earth/react-happy-global-state#readme
- Issues: https://github.com/react-earth/react-happy-global-state/issues
- npm.io page: https://npm.io/package/react-happy-global-state

## Dependencies (1)

- [object-standard-path](https://npm.io/package/object-standard-path.md) ^0.3.1

## 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.1.4 (latest) — 2024-09-07
- 0.1.3 — 2023-06-16
- 0.1.2 — 2023-06-13
- 0.1.1 — 2023-06-12

## README

![title](media/repo-header.svg)

<p align="center">
<a href="https://github.com/react-earth/react-happy-global-state"><img alt="star" src="https://img.shields.io/github/stars/react-earth/react-happy-global-state.svg?style=social&label=Star" /></a>
<a href="https://www.npmjs.com/package/react-happy-global-state"><img src="https://img.shields.io/npm/v/react-happy-global-state" alt="version"></a>
<a href="https://www.npmjs.com/package/react-happy-global-state"><img alt="minzip" src="https://img.badgesize.io/https:/unpkg.com/react-happy-global-state@latest/dist/index.esm.js?compression=gzip" /></a>
<a href="https://www.npmjs.com/package/react-happy-global-state"><img alt="downloads" src="https://img.shields.io/npm/dm/react-happy-global-state.svg" /></a>
<a href="https://github.com/react-earth/react-happy-global-state"><img alt="license" src="https://img.shields.io/npm/l/react-happy-global-state" /></a>
</p>

## Quick Features 🥳

- Manage your global state, just like using the useState hook.
- Built with typescript, provide type protection, code autocompletion, make your app robust.
- Less than 1kB size.

## How to use 📖

### Install package

```shell
npm install react-happy-global-state
```

### Create your store.ts

```tsx
import { createGlobalState } from 'react-happy-global-state';

// define you GlobalState type
type GlobalState = {
  count: number;
};

// set your default global state
const DEFAULT_GLOBAL_STATE: GlobalState = {
  count: 0,
};

export const { GlobalStateProvider, useGlobalState } = createGlobalState({
  defaultState: DEFAULT_GLOBAL_STATE,
});
```

### Use GlobalStateProvider to wrap your App

```tsx
import React from 'react';
import { createRoot } from 'react-dom/client';
import { App } from './App';
import { GlobalStateProvider } from './store';

const container = document.getElementById('root');
const root = createRoot(container!);

root.render(
  <React.StrictMode>
    {/* use GlobalStateProvider wrap your App  */}
    <GlobalStateProvider>
      <App />
    </GlobalStateProvider>
  </React.StrictMode>,
);
```

### Use useGlobalState in your Component

```tsx
import React from 'react';
import { useGlobalState } from './store';

export const Counter = () => {
  // use useGlobalState hook to get/set your global state
  const [count, setCount] = useGlobalState('count');

  return (
    <div>
      <h2>{count}</h2>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <button onClick={() => setCount(count - 1)}>Decrement</button>
    </div>
  );
};
```

Click [here](https://codesandbox.io/s/demo-5fvh56?file=/src/App.tsx) to try it by yourself.

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