# use-gun

> React Hooks for GunDB

Latest version **0.0.3** (published 2021-02-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-gun
pnpm add use-gun
yarn add use-gun
bun add use-gun
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2021-02-25 |
| First published | 2020-12-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Clark Tozer |
| Maintainers | clarktozer |
| Keywords | react, react-hooks, typescript, gundb |

## Links

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

## 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.0.3 (latest) — 2021-02-25
- 0.0.2 — 2020-12-22
- 0.0.1 — 2020-12-22

## README

# use-gun

![CI Status](https://img.shields.io/github/workflow/status/clarktozer/use-gun/CI)
[![npm version](https://img.shields.io/npm/v/use-gun.svg)](https://www.npmjs.com/package/use-gun)

React Hooks for GunDB

## GunProvider

A React provider which will allow access to the connect, disconnect functions, and the current gun instance using the "useGun" hook.

```js
import React, { FC } from "react";
import { GunProvider } from "use-gun";

const App: FC = ({ children }) => {
    const peers = ["https://events.mydomain.com"];

    return (
        <GunProvider peerUrls={peers}>
            <div className="my-app"><MyComponent /></div>
        </GunProvider>
    );
};
```

#### Options:

| option          | default | Description                                                           |
| --------------- | ------- | --------------------------------------------------------------------- |
| peerUrls        | -       | String array of urls for each. peer                                   |
| connectOnMount? | false   | Automatically call the connect function when the provider is mounted. |

## useGun

A React context for getting the connect, disconnect functions, and the current gun instance. Will only work if your app is wrapped in the GunProvider.

```js
import React, { FC, useEffect } from "react";
import { useGun } from "use-gun";

const MyComponent: FC = () => {
    const { gun, connect, disconnect } = useGun();

    useEffect(() => {
        connect();
    }, []);

    const onButtonClick = () => {
        disconnect();
    };

    return (
        <div className="my-component">
            <button onClick={onButtonClick}>Disconnect Gun</button>
        </div>
    );
};
```

## useGunWatch

A React hook for listening to changes on a property and providing the ability to unsubscribe.

```js
import React, { FC } from "react";
import { useGunWatch } from "use-gun";

const MyComponent: FC = () => {
    const [value, unsubscribe] = useGunWatch("my-key", 0);
    const [otherValue, unsubscribeOtherValue] = useGunWatch<string>("my-other-key");

    const onButtonClick = () => {
        unsubscribe();
    };

    return (
        <div className="my-component">
            <strong>{value}</strong>
            <button onClick={onButtonClick}>Unsubscribe Key</button>
        </div>
    );
};
```

#### Options:

| option        | default | Description                                                         |
| ------------- | ------- | ------------------------------------------------------------------- |
| key           | -       | Key for property to listen to.                                      |
| initialValue? | -       | Inital state value. If not value is passed, a generic can be added. |

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