npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hylozoic/react-holochain-hooks

v0.4.1

Published

React hooks integration of hc-web-client

Downloads

6

Readme

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>