# use-global-hook

> Easy state management for react using hooks in less than 1kb.

Latest version **0.3.0** (published 2021-07-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-global-hook
pnpm add use-global-hook
yarn add use-global-hook
bun add use-global-hook
```

## Health

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

Positive: has types package; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2021-07-23 |
| First published | 2019-03-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/use-global-hook) |
| Module format | ESM + CommonJS |
| Node | >=10.0.0 |
| Dependencies | 0 |
| Unpacked size | 23.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 649 |
| Author | andregardi |
| Maintainers | davit.yavryan, andregardi |

## Links

- npm: https://www.npmjs.com/package/use-global-hook
- Repository: https://github.com/use-global-hook/use-global-hook
- Homepage: https://github.com/use-global-hook/use-global-hook#readme
- Issues: https://github.com/use-global-hook/use-global-hook/issues
- npm.io page: https://npm.io/package/use-global-hook

## Recent versions

- 0.3.0 (latest) — 2021-07-23
- 0.1.13 (alpha) — 2020-08-08
- 0.2.5 — 2021-07-23
- 0.2.4 — 2021-07-22
- 0.2.3 — 2021-05-21
- 0.2.2 — 2021-04-01
- 0.2.1 — 2020-08-13
- 0.2.0 — 2020-08-13
- 0.1.12 — 2019-11-01
- 0.1.11 — 2019-09-16
- 0.1.10 — 2019-09-04
- 0.1.9 — 2019-09-02
- 0.1.8 — 2019-08-26
- 0.1.7 — 2019-04-15
- 0.1.6 — 2019-04-07
- … 4 more at https://npm.io/package/use-global-hook/versions

## README

# use-global-hook

Easy state management for react using hooks in less than 1kb.

------------
Table of Contents
* [Install](#install)
* [Example](#minimal-example)
* [Complete Examples](#complete-examples)
* [Using TypeScript](#using-typescript)

### Install:

```sh
npm i use-global-hook
```

or

```sh
yarn add use-global-hook
```

### Minimal example:
```jsx
import React from 'react';
import globalHook from 'use-global-hook';

const initialState = {
  counter: 0,
};

const actions = {
  addToCounter: (store, amount) => {
    const newCounterValue = store.state.counter + amount;
    store.setState({ counter: newCounterValue });
  },
};

const useGlobal = globalHook(initialState, actions);

const App = () => {
  const [globalState, globalActions] = useGlobal();
  return (
    <div>
      <p>
        counter:
        {globalState.counter}
      </p>
      <button type="button" onClick={() => globalActions.addToCounter(1)}>
        +1 to global
      </button>
    </div>
  );
};

export default App;
```

------------


### Complete examples:
#### [Several counters, one value](https://codesandbox.io/s/v6zz2nwow5 "CodeSandBox")
Add as many counters as you want, it will all share the same global value.
Every time one counter add 1 to the global value, all counters will render.
The parent component won't render again.


------------


#### [Asynchronous ajax requests](https://codesandbox.io/s/wqvykj5497 "CodeSandBox")
Search GitHub repos by username.
Handle the ajax request asynchronously with async/await.
Update the requests counter on every search.


------------


#### [Avoid unnecessary renders](https://codesandbox.io/s/several-counters-pdbsy "CodeSandBox")
Map a subset of the global state before use it.
The component will only re-render if the subset is updated.

------------


#### [Connecting to a class component](https://codesandbox.io/s/connect-a-class-component-rgbf1 "CodeSandBox")
Hooks can't be used inside a class component.
We can create a Higher-Order Component that connects any class component with the state.
With the connect() function, state and actions become props of the component.


------------


#### [Immutable state with Immer.js integration](https://codesandbox.io/s/immer-integration-e1hpj "CodeSandBox")
Add Immer.js lib on your hook options to manage complex immutable states.
Mutate a state draft inside a setState function.
Immer will calculate the state diff and create a new immutable state object.


------------

### Using TypeScript

Install the TypeScript definitions from DefinitelyTyped
```
npm install @types/use-global-hook
```

Example implementation
```typescript
import globalHook, { Store } from 'use-global-hook';

// Defining your own state and associated actions is required
type MyState = {
  value: string;
};

// Associated actions are what's expected to be returned from globalHook
type MyAssociatedActions = {
  setValue: (value: string) => void;
  otherAction: (other: boolean) => void;
};

// setValue will be returned by globalHook as setValue.bind(null, store)
// This is one reason we have to declare a separate associated actions type
const setValue = (
  store: Store<MyState, MyAssociatedActions>,
  value: string
) => {
  store.setState({ ...store.state, value });
  store.actions.otherAction(true);
};

const otherAction = (
  store: Store<MyState, MyAssociatedActions>,
  other: boolean
) => { /* cool stuff */ };

const initialState: MyState = {
  value: "myString"
};

// actions passed to globalHook do not need to be typed
const actions = {
  setValue,
  otherAction
};

const useGlobal = globalHook<MyState, MyAssociatedActions>(
  initialState,
  actions
);

// Usage
const [state, actions] = useGlobal<MyState, MyAssociatedActions>();

// Subset
const [value, setValue] = useGlobal<string, (value: string) => void>(
  (state: MyState) => state.value,
  (actions: MyAssociatedActions) => actions.setValue
);

// Without declaring type, useGlobal will return unknown
const [state, actions] = useGlobal(); // returns [unknown, unknown]

// Happy TypeScripting!
```

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