React hooks for the hypercore-protocol stack
npm i use-hyper
Warning: this is experimental, and API might unexpectedly change until v1.
Every hook requires the related library installed:
useCoredepends onhypercore.useDHTdepends on@hyperswarm/dht-relay.useSwarmdepends onhyperswarm.
If you import useSwarm then install this specific branch:npm i holepunchto/hyperswarm#add-swarm-session
import{useCore,useCoreWatch,useCoreEvent}from'use-hyper/core'import{DHT,useDHT}from'use-hyper/dht'import{Swarm,useSwarm,useReplicate}from'use-hyper/swarm'importRAMfrom'random-access-memory'constChild=()=>{const{ core }=useCore()// Gets core from contextconst{ onwatch }=useCoreWatch()// Triggers re-render when core changesconst{onwatch: onappend}=useCoreWatch(['append'])// Same as aboveuseCoreEvent('append',()=>console.log('on event',core.length))useReplicate(core)constDHT=useDHT()// Gets DHT from the contextconstswarm=useSwarm()// Same, from contextreturn(<div>
ID {core.id}<br/>
Length {core.length}<br/>
Peers {core.peers.length}</div>)}constApp=()=>{return(<Corestorage={RAM}publicKey={key}><Child/></Core>)}exportdefault()=>{return(<DHT><Swarm><App/></Swarm></DHT>)}Every state like core, dht, or swarm starts being null but then gets updated with the corresponding object.
MIT