# preact-global-state

> Global State for Preact

Latest version **1.0.5** (published 2022-06-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install preact-global-state
pnpm add preact-global-state
yarn add preact-global-state
bun add preact-global-state
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2022-06-10 |
| First published | 2021-02-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 4.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Maintainers | supunkavinda |
| Keywords | preact, state |

## Links

- npm: https://www.npmjs.com/package/preact-global-state
- Repository: https://github.com/SupunKavinda/preact-global-state
- Homepage: https://github.com/SupunKavinda/preact-global-state/blob/master/README.md
- Issues: https://github.com/SupunKavinda/preact-global-state/issues
- npm.io page: https://npm.io/package/preact-global-state

## Dependencies (1)

- [preact](https://npm.io/package/preact.md) ^10.5.12

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 1.0.5 (latest) — 2022-06-10
- 1.0.4 — 2021-06-03
- 1.0.3 — 2021-06-03
- 1.0.2 — 2021-06-03
- 1.0.1 — 2021-02-25
- 1.0.0 — 2021-02-25
- 0.3.0 — 2021-02-25

## README

This is a simple version (79 lines, 200 bytes minified gziped) of [React state-pool](https://github.com/yezyilomo/state-pool). This only contains one Preact hook, `useGlobalState` that can be used to set/get global states.

## Installing

```
npm install preact-global-state
```

## Usage

```js
import { store, useGlobalState } from 'preact-global-state';

store.init({
    name: "Ghost"
});

function App() {
    const [name, setName] = useGlobalState('name');

    return (
        <div>
            The Name: { name }
            <button onClick={() => setName('Another name')}></button>   
        </div>
    )
}

// another component
function Compo2() {
    const [name] = useGlobalState('name');

    return <div>{name}</div>
}
```

Whenever the button is clicked, all components that use the `name` globalState will be updated (Ex: Compo2).

* `store.init` sets the starting values.
* You can also set a default value as `useGlobalState(key, defaultValue)`, which will be used when a global state for that key is not defined.

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