Socket
Book a DemoInstallSign in
Socket

@hylozoic/react-holochain-hooks

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hylozoic/react-holochain-hooks

React hooks integration of hc-web-client

0.4.1
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

react-holochain-hooks

Connect to a running Holochain conductor using a React hook.

Heads up

This is an early version. It works fine, but there is still work to be done on the API, so expect it to change.

Installation

npm install @hylozoic/react-apollo-hooks

Example App

Zome Explorer uses this module. See App.js.

Usage

Basic usage

This is a React hook, so follow the rules of hooks.

import useHolochainConnection from '@hylozoic/react-holochain-hooks'

function MyComponent () {
  const { callZomeRef } = useHolochainConnection(CONDUCTOR_WEBSOCKET_URL)

  const callSpecificZomeFunction = callZomeRef.current(INSTANCE_ID, ZOME_NAME, ZOME_FUNCTION_NAME)

  return <div>
    <div onClick={() => callSpecificZomeFunction({param1: "a", param2: "b"})}>Go</div>
  </div>

}

Where CONDUCTOR_WEBSOCKET_URL is the url from the websocket interface you set up in your conductor-config.toml file and INSTANCE_ID is the instance id from the same file.

Details

useHolochainConnection returns an object with four keys.

const { callZomeRef, callRef, closeRef, wsRef } = useHolochainConnection(CONDUCTOR_WEBSOCKET_URL)

callZomeRef for calling zome functions. It takes ('instanceId', 'zome', 'funcName') and returns a function which when called with a params object, calls the specified zome function and returns a promise with the result

callRef for calling conductor functions. It takes a string which is the path to the conductor function and returns a function like above.

closeRef for closing the connection.

wsRef the websocket object (kind of. Actually the object returned by rpc-websockets which is a wrapper for a webscoket and behaves like one in some ways)

These are all React refs, so to get at the actual function or object you say callZome.current()

These are the functions and objects returned from hc-web-client.connect, so read the docs there for more details

Useless wsRef

Because of the interaction between asyncronous hooks and refs, wsRef is not super useful at the moment. The main thing you want the websocket object is to access the ready state, so for now you can use a callback. See Using ready state below for details. I'm currently looking in to a better solution for this, and this is the part of the API most likely to change in the future.

Configuring connection

The second argument to useHolochainConnection is a config object that's passed to rpc-websockets. See those docs for more details

const wsClientConfig = {
  autoconnect = true,
  reconnect = true,
  reconnect_interval = 1000,
  max_reconnects = 5
}
const { callZomeRef } = useHolochainConnection(CONDUCTOR_WEBSOCKET_URL, wsClientConfig)

Using ready state

The third argument to useHolochainConnection is a callback function that is called when the state of the connection changes. It's passed true if the connection is ready, otherwise false.

const [ready, setReady] = useState(false)
const { callZomeRef } = useHolochainConnection(CONDUCTOR_WEBSOCKET_URL, {}, setReady)

return <div>
  {ready ? "It's ready!" : "Not ready yet"}
</div>

FAQs

Package last updated on 19 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.