# @golem-sdk/react

> React hooks for working with the Golem Network

Latest version **4.0.0** (published 2024-07-24) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install @golem-sdk/react
pnpm add @golem-sdk/react
yarn add @golem-sdk/react
bun add @golem-sdk/react
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2024-07-24 |
| First published | 2023-10-04 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 110.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | GolemFactory |
| Maintainers | mgordel, grisha87, pangrzegorz, prekucki |
| Keywords | golem, network, sdk, react, hooks, distributed, computing, internet, computer, marketplace |

## Links

- npm: https://www.npmjs.com/package/@golem-sdk/react
- Repository: https://github.com/golemfactory/golem-sdk-react
- Homepage: https://github.com/golemfactory/golem-sdk-react#readme
- Issues: https://github.com/golemfactory/golem-sdk-react/issues
- npm.io page: https://npm.io/package/@golem-sdk/react

## Dependencies (1)

- [swr](https://npm.io/package/swr.md) ^2.2.2

## 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

- 4.0.0 (latest) — 2024-07-24
- 3.0.1 — 2024-02-27
- 3.0.0 — 2024-02-13
- 2.0.0 — 2023-11-14
- 1.0.2 — 2023-10-26
- 1.0.1 — 2023-10-04
- 1.0.0 — 2023-10-04

## README

# Golem React SDK

A collection of React hooks for interacting with the Golem Network.

## Installation

Get started by installing the core package and it's peer dependencies:

```
npm install @golem-sdk/react @golem-sdk/task-executor@2 @golem-sdk/golem-js@3
```

Then make sure to wrap your app with the `YagnaProvider` component:

```jsx
<YagnaProvider>
  <App />
</YagnaProvider>
```

### Initial Configuration

For the SDK to work properly you need to have a running Yagna instance on your local machine. Please follow the [Yagna installation guide](https://docs.golem.network/docs/creators/javascript/examples/tools/yagna-installation-for-requestors) to install and configure Yagna.

Yagna blocks all requests from external origins by default. To allow the SDK to communicate with it you need start Yagna with `--api-allow-origin='<your-domain>'` flag. For example:

```shell
yagna service run --api-allow-origin='http://localhost:3000'
```

## Demo application

Sometimes it's easier to learn by example. That's why we've created a demo application that uses all the hooks from the SDK. You can find it in the `examples/react-with-vite` directory. To run it locally, first clone the repository, install it's dependencies and build the react package:

```
git clone https://github.com/golemfactory/golem-sdk-react.git
cd golem-sdk-react/
npm install
npm run build
```

Then run the following command to start the demo application:

```
npm run example:vite
```

If you just want to see how the demo application looks like, you can check out the live version [here](https://golem-react-showcase.vercel.app/).

## Features

### `useYagna` - check if you're connected to Yagna and manage the app-key

```jsx
function MyComponent() {
  const { isConnected, appKey, setYagnaOptions } = useYagna();
  const inputRef = useRef(null);
  return (
    <div>
      <div>Connected to Yagna: {isConnected ? "yes" : "no"}</div>
      <input ref={inputRef} />
      <button
        onClick={() => setYagnaOptions({ apiKey: inputRef.current.value })}
      >
        Set app key
      </button>
    </div>
  );
}
```

### `useExecutor` + `useTask` - run a task on the Golem Network

```jsx
function MyTask({ executor }) {
  const { isRunning, error, result, run } = useTask(executor);
  const onClick = () =>
    run(async (exe) => {
      return (await exe.run("echo", ["Hello world!"])).stdout;
    });
  return (
    <div>
      <button onClick={onClick} disabled={isRunning}>
        Run task
      </button>
      {isRunning && <div>Task is running...</div>}
      {error && <div>Task failed due to {error.toString()}</div>}
      {result && <div>Task result: {result}</div>}
    </div>
  );
}

function MyComponent() {
  const {
    executor,
    initialize,
    isInitialized,
    isInitializing,
    terminate,
    error,
  } = useExecutor({
    demand: {
      workload: { imageTag: "golem/alpine:latest" },
    },
    market: {
      rentHours: 0.5,
      pricing: {
        model: "linear",
        maxStartPrice: 0.5,
        maxCpuPerHourPrice: 1.0,
        maxEnvPerHourPrice: 0.5,
      },
    },
  });
  if (isInitializing) {
    return <div>Initializing executor...</div>;
  }
  if (error) {
    return <div>Error: {error.toString()}</div>;
  }
  if (!isInitialized) {
    return (
      <div>
        <button onClick={initialize}>Initialize executor</button>
      </div>
    );
  }
  return (
    <div>
      <MyTask executor={executor} />
      <button onClick={terminate}>Terminate executor</button>
    </div>
  );
}
```

### `useInvoices` + `useHandleInvoice` - list and handle invoices

```jsx
function Invoice({ invoiceId }) {
  const { acceptInvoice } = useHandleInvoice(invoiceId);

  return (
    <li key={invoice.invoiceId}>
      {invoice.invoiceId} - {invoice.status}
      <button onClick={acceptInvoice} disabled={invoice.status !== "RECEIVED"}>
        Accept
      </button>
    </li>
  );
}

function MyComponent() {
  const { invoices, isLoading, error, refetch } = useInvoices({
    limit: 10,
    statuses: ["RECEIVED"],
    after: new Date("2021-01-01"),
  });
  if (isLoading) {
    return <div>Loading...</div>;
  }
  if (error) {
    return <div>Error: {error.toString()}</div>;
  }
  return (
    <div>
      <ul>
        {invoices.map((invoice) => (
          <Invoice key={invoice.invoiceId} invoiceId={invoice.invoiceId} />
        ))}
      </ul>
      <button onClick={refetch}> Refresh </button>
    </div>
  );
}
```

## Developing

If you want to contribute to the SDK, you can clone the repository and install the dependencies:

```shell
git clone https://github.com/golemfactory/golem-sdk-react.git
cd golem-sdk-react/
npm install
```

## Links

- Golem documentation: https://docs.golem.network/
- Repository: https://github.com/golemfactory/golem-sdk-react/
- Issue tracker: https://github.com/golemfactory/golem-sdk-react/issues
  - In case of sensitive bugs like security vulnerabilities, please contact
    contact@golem.network directly instead of using issue tracker. We value your effort to improve the security and privacy of this project!
- Related projects:
  - Golem JavaScript API: https://github.com/golemfactory/golem-js/

## Licensing

The code in this project is licensed under GPL-3.0 license.

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