# automerge-repo-react-hooks

> Hooks to access an Automerge Repo from your react app.

Latest version **0.1.0** (published 2023-06-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install automerge-repo-react-hooks
pnpm add automerge-repo-react-hooks
yarn add automerge-repo-react-hooks
bun add automerge-repo-react-hooks
```

## Health

**Score 40/100 (D)** — status: abandoned.

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

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2023-06-17 |
| First published | 2022-09-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 718 |
| Author | Peter van Hardenberg |
| Maintainers | pvh |

## Links

- npm: https://www.npmjs.com/package/automerge-repo-react-hooks
- Repository: https://github.com/pvh/automerge-repo
- Homepage: https://github.com/pvh/automerge-repo#readme
- Issues: https://github.com/pvh/automerge-repo/issues
- npm.io page: https://npm.io/package/automerge-repo-react-hooks

## Dependencies (1)

- [automerge-repo](https://npm.io/package/automerge-repo.md) ^0.1.0

## Recent versions

- 0.1.0 (latest) — 2023-06-17
- 0.0.14 (wasm) — 2022-10-01
- 0.0.56 — 2023-06-10
- 0.0.55 — 2023-06-09
- 0.0.54 — 2023-05-25
- 0.0.53 — 2023-05-19
- 0.0.52 — 2023-05-13
- 0.0.51 — 2023-05-13
- 0.0.50 — 2023-03-16
- 0.0.48 — 2023-02-15
- 0.0.45 — 2023-02-03
- 0.0.44 — 2023-02-01
- 0.0.42 — 2022-12-30
- 0.0.32 — 2022-12-30
- 0.0.41 — 2022-12-14
- … 8 more at https://npm.io/package/automerge-repo-react-hooks/versions

## README

# React Hooks for Automerge Repo

## Example usage

### App Setup

```ts
import React, { StrictMode } from "react"
import ReactDOM from "react-dom/client"
import localforage from "localforage"

import { Repo, DocCollection } from "automerge-repo"

import { BroadcastChannelNetworkAdapter } from "automerge-repo-network-broadcastchannel"
import { LocalFirstRelayNetworkAdapter } from "automerge-repo-network-localfirstrelay"

import App, { RootDocument } from "./App.js"
import { RepoContext } from "automerge-repo-react-hooks"

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const sharedWorker = new SharedWorker(
  new URL("./shared-worker.js", import.meta.url),
  { type: "module", name: "automerge-repo-shared-worker" }
)

async function getRepo(url: string): Promise<DocCollection> {
  return await Repo({
    network: [
      new BroadcastChannelNetworkAdapter(),
      new LocalFirstRelayNetworkAdapter("wss://local-first-relay.glitch.me/"),
    ],
    sharePolicy: peerId => peerId.includes("shared-worker"),
  })
}

async function getRootDocument(repo: DocCollection, initFunction: any) {
  let docId: string | null = window.location.hash.replace(/^#/, "")
  if (!docId) {
    docId = await localforage.getItem("root")
  }
  let rootHandle

  if (!docId) {
    rootHandle = repo.create()
    rootHandle.change(initFunction)
    await localforage.setItem("root", rootHandle.documentId)
  } else {
    rootHandle = await repo.find(docId)
    window.location.hash = docId
  }
  return rootHandle
}

const initFunction = (d: RootDocument) => {
  d.items = []
}

const queryString = window.location.search // Returns:'?q=123'

// Further parsing:
const params = new URLSearchParams(queryString)
const hostname = params.get("host") || "automerge-storage-demo.glitch.me"

getRepo(`wss://${hostname}`).then(repo => {
  getRootDocument(repo, initFunction).then(rootDoc => {
    const rootElem = document.getElementById("root")
    if (!rootElem) {
      throw new Error("The 'root' element wasn't found in the host HTML doc.")
    }
    const root = ReactDOM.createRoot(rootElem)
    root.render(
      <StrictMode>
        <RepoContext.Provider value={repo}>
          <App rootDocumentId={rootDoc.documentId} />
        </RepoContext.Provider>
      </StrictMode>
    )
  })
})
```

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