facebookarchive / draft-js

A React framework for building text editors.
https://draftjs.org/
MIT License
22.56k stars 2.64k forks source link

Replace the text {{timer}} with react component #3140

Open waqas61 opened 2 years ago

waqas61 commented 2 years ago

Using reactStringReplace react node package and dangerouslySetInnerHTML to replace the specific text with react component

Editor content text to replace {{timer}} good day

    this.onChange = (editorState) => {

        this.setState({
            editorState,
            editorContentHtml: convertToHTML(
                editorState.getCurrentContent()
            ),
        });

        let replacedText;
        replacedText = reactStringReplace(
            this.state.editorContentHtml,
            /({{timer}})/g,
            (match, i) => (
                <EventCountdown value="2022-06-10T00:00:00+01:00" />
            )
        );

        this.setState({
            template: replacedText,
        });
    };
      <p
          dangerouslySetInnerHTML={{
              __html: this.state.template,
          }}
      ></p>

it does not work. In Dom, it looks like text to replace [object Object] good day

rrijvy commented 1 year ago

where did you get that convertToHTML