Using React
By the end of this page you have a board in a React app that saves itself and a button that picks a tool.
Install
npm install @fieldnotes/core @fieldnotes/react
@fieldnotes/react needs React 18 or 19 and @fieldnotes/core >=0.110.0 <1.0.0.
Mount a board
<FieldNotesCanvas> creates the viewport when it mounts and destroys it when it unmounts. With no tools prop, and no tools registered in onReady, it registers the default tools and starts on select, as tools: 'default' does in Getting started.
const saved = localStorage.getItem('board') ?? undefined;
const save = (state: CanvasState) => localStorage.setItem('board', JSON.stringify(state));
export function App() {
return (
<FieldNotesCanvas initialData={saved} onChange={save} style={{ height: '100vh' }}>
<PencilButton />
</FieldNotesCanvas>
);
}
initialData takes a saved board as a CanvasState or a JSON string. onChange receives viewport.exportState() 250ms after the last change; set changeDebounceMs to change the wait. Loading initialData does not call onChange, and a pending call runs on unmount, so the last edit is not lost.
Give <FieldNotesCanvas> a height through style or className. It renders one div, and the board fills it.
The theme prop sets the board theme and applies later changes. Left undefined,
it keeps whatever theme the viewport has, including one set with setTheme. Hoist a theme object
out of the render.
Use the viewport from a component
Children of <FieldNotesCanvas> can call the hooks. useActiveTool returns the active tool’s name and a setter:
function PencilButton() {
const [tool, setTool] = useActiveTool();
return (
<button type="button" aria-pressed={tool === 'pencil'} onClick={() => setTool('pencil')}>
Pencil
</button>
);
}
useViewport() returns the Viewport itself, for anything the hooks don’t cover. It throws outside <FieldNotesCanvas>. From outside, use the onReady(viewport) prop or a ref. useHistory, useLayers, useSelection and useCamera re-render the component when undo state, layers, the selection or the camera change. useExport returns exportJSON, exportImage, exportSVG and exportPDF, and useLoad loads a saved board (Export, Saving and loading). useCameraControls moves the camera, and useCanvasSize returns the board’s size in CSS pixels. useShortcuts reads and rebinds keyboard shortcuts. useElement(id) returns one element. useTransaction makes several changes one undo step. usePresence shows who else is on the board.
React components on the board
<HostElement position={{ x, y }}> renders its children on the board, where they pan and zoom with it. React owns the element: moving it creates no undo step, and exportState() leaves it out of the saved board.
The @fieldnotes/react README says which props are read only at mount, and covers custom tools and the selection hooks.
@fieldnotes/vue, @fieldnotes/svelte and @fieldnotes/angular export FieldNotesCanvas, CanvasElement (HostElement in React) and Minimap, and helpers with the React hook names that give their state as Vue refs, Svelte stores and Angular signals. Angular changes use to inject in the names (injectCamera).
Save the board automatically
autoSave={{ key: 'board' }} loads the board saved under that key after onReady, then saves 1000ms after the last change, to localStorage unless you pass an adapter. A saved board replaces initialData and anything onReady added. Saving and loading lists the options.
export function AutoSaveApp() {
return <FieldNotesCanvas autoSave={{ key: 'board' }} style={{ height: '100vh' }} />;
}
Shortcuts
useShortcuts() returns the current bindings, re-renders when one changes, and gives rebind, disable and reset. A hoisted shortcuts prop sets bindings (Keyboard shortcuts).
export function UndoShortcut() {
const { bindings } = useShortcuts();
return <span>{bindings['edit.undo']?.join(', ') ?? ''}</span>;
}
Presence
usePresence(client, { identity }) draws other people’s cursors and selections and returns them as peers (README).