Skip to main content

EditTextReactCanvas

EditTextReactCanvas renders component code and enables text editing behavior for rendered text nodes.

import { EditTextReactCanvas } from 'react-code-canvas';

<EditTextReactCanvas
code={code}
scope={scope}
showPreview={true}
showEditor={false}
showError={true}
onSaveFinalCode={(updatedCode) => {
setCode(updatedCode);
}}
onError={(error) => {
setError(error);
}}
/>;

Props

PropTypeDescription
codestringReact code to render and transform for editing.
scopeRecord<string, any>Values available to the rendered code.
showPreviewbooleanShows the rendered output. Defaults to true.
showEditorbooleanShows the code editor. Defaults to false.
showErrorbooleanShows runtime or transform errors. Defaults to false.
onSaveFinalCode(jsxCode: string) => voidReceives updated JSX after text edits.
onError(error: string) => voidReceives transform errors, render errors, and the "rendered nothing" case.
persistKeystringOptional localStorage key. When set, the final saved JSX is persisted and restored on reload.
onCodeChange(jsxCode: string) => voidCalled with the final JSX whenever it changes, alongside onSaveFinalCode.
showLoaderbooleanShows a full-screen overlay while non-empty code has not produced output yet. Defaults to !showEditor.
loaderReact.ReactNodeReplaces the default spinner with your own node.
errorComponentReact.ReactNode | ((error: string, dismiss: () => void) => React.ReactNode)Replaces the built-in error toast. Receives the message and a dismiss callback. Requires showError.
dismissibleErrorbooleanShows a close button on the built-in toast. Defaults to true.

Persisting code

Pass a persistKey to save and restore edits across reloads via localStorage:

<EditTextReactCanvas code={code} persistKey="my-editable-draft" />

Persistence here operates on the final saved JSX (the clean output), not the intermediate EditableText representation — so a restored value transforms back into edit mode correctly. Use onCodeChange if you want to persist the final JSX in your own store instead.

Loading state

showLoader and loader behave exactly as in ReactCanvas, including the !showEditor default and the deferred initial evaluation that lets the loader paint. Note that this component transforms the incoming code before rendering it, so the overlay also covers that transform pass.

Status

This API is still evolving. Treat it as beta and test it with the shapes of JSX your product generates.