High-Performance UI Component Library for Enterprise Applications › Forums › Editor & Inputs › How do I handle two-way binding with Smart Editor in web app? › Reply To: How do I handle two-way binding with Smart Editor in web app?
April 28, 2026 at 2:21 pm
#113539
Keymaster
Hi,
You usually have to simulate two-way binding by explicitly syncing incoming props → editor state and editor changes → your app state, while guarding against loops and stale updates.
You can try this code:
function SmartEditorWrapper({ value, onChange }) {
const editorRef = useRef(null);
const lastValueRef = useRef(value);
// Initialize editor once
useEffect(() => {
editorRef.current = createSmartEditor({
content: value,
onUpdate: (newContent) => {
// prevent unnecessary updates
if (newContent !== lastValueRef.current) {
lastValueRef.current = newContent;
onChange(newContent);
}
}
});
return () => editorRef.current?.destroy();
}, []);
// Sync external value → editor
useEffect(() => {
if (!editorRef.current) return;
if (value !== lastValueRef.current) {
lastValueRef.current = value;
editorRef.current.setContent(value);
}
}, [value]);
return <div id="editor-container" />;
}
Regards,
Peter