Skip to content

Latest commit

History

22 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

use-hyper

React hooks for the hypercore-protocol stack

npm i use-hyper

Warning: this is experimental, and API might unexpectedly change until v1.

Usage

Every hook requires the related library installed:

  • useCore depends on hypercore.
  • useDHT depends on @hyperswarm/dht-relay.
  • useSwarm depends on hyperswarm.

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.

License

MIT

About

React hooks for the hypercore-protocol stack

Resources

Stars

15 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages