Skip to content

React Hooks (Legacy)

WARNING

This documents the old Node.js API. The @vibecook/truffle-react package needs updating to work with the v2 architecture (RFC 012).

bash
npm install @vibecook/truffle-react

Peer dependency: react >= 18

useMesh

Subscribe to mesh node state.

typescript
import { useMesh } from '@vibecook/truffle-react';

function MeshStatus({ node }: { node: MeshNode | null }) {
  const {
    devices,
    localDevice,
    isConnected,
    broadcast,
    sendTo,
  } = useMesh(node);

  return (
    <div>
      <p>Status: {isConnected ? 'Connected' : 'Disconnected'}</p>
      <p>Devices: {devices.length}</p>
      <ul>
        {devices.map(d => (
          <li key={d.id}>{d.name} - {d.status}</li>
        ))}
      </ul>
    </div>
  );
}

Parameters

ParamTypeDescription
nodeMeshNode | nullThe mesh node instance (pass null before initialization)

Return Value

FieldTypeDescription
devicesBaseDevice[]All known remote devices
localDeviceBaseDevice | nullLocal device info
isConnectedbooleanWhether the node is running
broadcast(ns, type, payload) => voidBroadcast a message
sendTo(deviceId, ns, type, payload) => booleanSend to a device

useSyncedStore

Subscribe to a synced store's local data.

typescript
import { useSyncedStore } from '@vibecook/truffle-react';

function DataView({ store }: { store: ISyncableStore<MyData> }) {
  const { localData, localSlice, version } = useSyncedStore(store);

  if (!localData) return <p>No data</p>;

  return (
    <div>
      <p>Version: {version}</p>
      <pre>{JSON.stringify(localData, null, 2)}</pre>
    </div>
  );
}

Parameters

ParamTypeDescription
storeISyncableStore<T> | nullThe syncable store instance

Return Value

FieldTypeDescription
localSliceDeviceSlice<T> | nullFull local slice
localDataT | nullShortcut to localSlice.data
versionnumberCurrent version (0 if no slice)

Released under the MIT License.