react/README.md

100 lines
1.9 KiB
Markdown
Raw Normal View History

2020-04-24 16:38:24 +02:00
# @oceanprotocol/react
> React hooks & components on top of squid.js
## Installation
```bash
npm install @oceanprotocol/react
```
## Usage
2020-04-24 16:49:59 +02:00
First, wrap your App with the `OceanProvider`:
```tsx
import React from 'react'
import { OceanProvider } from '@oceanprotocol/react'
export default function MyApp({ children }: { children: any }) {
// TODO: setup web3 first
2020-04-24 18:01:06 +02:00
// or fallback to injected providers by default
// so it works with any browser wallet out of the box
2020-04-24 16:49:59 +02:00
return (
<OceanProvider web3={web3}>
2020-04-24 16:49:59 +02:00
<h1>My App</h1>
{children}
</OceanProvider>
)
}
```
Then within your component use the provided hooks to interact with Ocean's functionality:
2020-04-24 16:38:24 +02:00
```tsx
import React from 'react'
import { useOcean, OceanConfig, useConsume } from '@oceanprotocol/react'
const oceanConfig: OceanConfig = {
nodeUri: '',
...
}
export default function MyComponent() {
2020-04-24 18:01:06 +02:00
// Initialize, get existing, or reinitialize Ocean
const { ocean, account } = useOcean(oceanConfig)
2020-04-24 16:49:59 +02:00
// consume asset
const { consumeAsset, isLoading, step } = useConsume(ocean)
2020-04-24 16:38:24 +02:00
async function handleClick() {
const ddo = 'did:op:0x000000000'
await consumeAsset(ddo, account)
2020-04-24 16:38:24 +02:00
}
return (
<div>
Your account: {account}
<button onClick={handleClick}>
{isLoading ? step : 'Download Asset' }
</button>
</div>
)
}
2020-04-24 18:01:06 +02:00
```
### Specs
#### `useOcean()`
```tsx
interface UseOcean {
ocean: Ocean
account: string
balance: { ocean: string, eth: string }
status: OceanConnectionStatus
}
const result: UseOcean = useOcean(config: OceanConfig)
```
#### `useConsume()`
```tsx
interface ConsumeOptions {
ocean: Ocean
}
interface UseConsume {
consumeAsset: (ddo: DDO, account: string) => void
isLoading: boolean
step: number
error: string | undefined
}
const result: UseConsume = useConsume(options: ConsumeOptions)
2020-04-24 16:38:24 +02:00
```