lexicaljs

How to add text programmatically with Lexical editor?


I'm using draftjs so far and I like it but since draftjs is going to be retired I'm playing with lexical text editor. But I could not figure out even very basic thing, insert text programmatically.

How can I insert text programmatically? like draftjs's Modifier.insertText?

Thanks for advance. example codes or link to examples are very helpful.


Solution

  • editor.update does the trick: https://lexical.dev/docs/concepts/editor-state#updating-state

    Here is a working example with React: https://codesandbox.io/s/lexical-update-d035dn?file=/src/Editor.js