#113539
admin
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