@groundstate/react
v1.0.0-beta
Published
React bindings for @groundstate
Maintainers
Readme
@groundstate/react
React hooks and components for building local-first apps with @groundstate.
Installation
# npm
npm install @groundstate/react @groundstate/crdt
# yarn
yarn add @groundstate/react @groundstate/crdt
# pnpm
pnpm add @groundstate/react @groundstate/crdtPeer dependency: React 18+
Quick Start
import { GroundstateProvider, useDoc, useField, usePresence } from '@groundstate/react';
import { Doc, Field, List } from '@groundstate/crdt';
const schema = { title: Field(''), tags: List<string>() };
function App() {
return (
<GroundstateProvider doc={Doc(schema)} sync={syncEngine}>
<TodoEditor />
</GroundstateProvider>
);
}
function TodoEditor() {
const doc = useDoc();
const [title, setTitle] = useField(doc, 'title');
const { peers } = usePresence();
return (
<div>
<input value={title} onChange={(e) => setTitle(e.target.value)} />
<p>{peers.length} peer(s) online</p>
</div>
);
}API Highlights
Provider
GroundstateProvider-- Context provider that supplies the document and sync engineGroundstateContext-- Raw React context for advanced use casesuseGroundstate()-- Access the provider context directly
Hooks
useDoc()-- Subscribe to the full document, re-renders on any changeuseField(doc, path)-- Subscribe to a single field with a setterusePresence()-- Track peer presence (cursors, selections, names)useSyncStatus()-- Monitor sync connection state and progressuseConflicts()-- Access detected conflicts for manual resolution
Components
ConflictResolver-- Drop-in UI component for resolving conflicts interactively
Documentation
See the full documentation for complete API reference.
License
MIT
